<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>

<meta charset="utf-8">
<meta name="generator" content="quarto-1.7.6">

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

<meta name="dcterms.date" content="2025-02-22">

<title>R语言常用函数手册</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
  width: 0.8em;
  margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */ 
  vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
  }
pre.numberSource { margin-left: 3em;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>


<script src="R语言常用函数_files/libs/clipboard/clipboard.min.js"></script>
<script src="R语言常用函数_files/libs/quarto-html/quarto.js"></script>
<script src="R语言常用函数_files/libs/quarto-html/popper.min.js"></script>
<script src="R语言常用函数_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="R语言常用函数_files/libs/quarto-html/anchor.min.js"></script>
<link href="R语言常用函数_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="R语言常用函数_files/libs/quarto-html/quarto-syntax-highlighting-6c21014d9765ba549c6ebad95917e4d7.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="R语言常用函数_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="R语言常用函数_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="R语言常用函数_files/libs/bootstrap/bootstrap-181e825af9c884036bc55433e5bb67d1.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">


</head>

<body>

<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
  <nav id="TOC" role="doc-toc" class="toc-active">
    <h2 id="toc-title">Table of contents</h2>
   
  <ul>
  <li><a href="#数据结构" id="toc-数据结构" class="nav-link active" data-scroll-target="#数据结构">数据结构</a>
  <ul class="collapse">
  <li><a href="#一数据管理" id="toc-一数据管理" class="nav-link" data-scroll-target="#一数据管理">一、数据管理</a></li>
  </ul></li>
  <li><a href="#字符串处理" id="toc-字符串处理" class="nav-link" data-scroll-target="#字符串处理">字符串处理</a>
  <ul class="collapse">
  <li><a href="#一字符型向量" id="toc-一字符型向量" class="nav-link" data-scroll-target="#一字符型向量">一、字符型向量</a></li>
  <li><a href="#二字符数" id="toc-二字符数" class="nav-link" data-scroll-target="#二字符数">二、字符数</a></li>
  <li><a href="#三取子串" id="toc-三取子串" class="nav-link" data-scroll-target="#三取子串">三、取子串</a></li>
  <li><a href="#四连接或拆分" id="toc-四连接或拆分" class="nav-link" data-scroll-target="#四连接或拆分">四、连接或拆分</a></li>
  </ul></li>
  <li><a href="#复数" id="toc-复数" class="nav-link" data-scroll-target="#复数">复数</a>
  <ul class="collapse">
  <li><a href="#一复数函数" id="toc-一复数函数" class="nav-link" data-scroll-target="#一复数函数">一、复数函数</a></li>
  </ul></li>
  <li><a href="#因子" id="toc-因子" class="nav-link" data-scroll-target="#因子">因子</a>
  <ul class="collapse">
  <li><a href="#一因子" id="toc-一因子" class="nav-link" data-scroll-target="#一因子">一、因子</a></li>
  <li><a href="#二因子的水平" id="toc-二因子的水平" class="nav-link" data-scroll-target="#二因子的水平">二、因子的水平</a></li>
  </ul></li>
  <li><a href="#数学相关计算" id="toc-数学相关计算" class="nav-link" data-scroll-target="#数学相关计算">数学相关计算</a>
  <ul class="collapse">
  <li><a href="#一基本计算" id="toc-一基本计算" class="nav-link" data-scroll-target="#一基本计算">一、基本计算</a></li>
  <li><a href="#二数学函数" id="toc-二数学函数" class="nav-link" data-scroll-target="#二数学函数">二、数学函数</a></li>
  </ul></li>
  <li><a href="#数组" id="toc-数组" class="nav-link" data-scroll-target="#数组">数组</a>
  <ul class="collapse">
  <li><a href="#一数组创建" id="toc-一数组创建" class="nav-link" data-scroll-target="#一数组创建">一、数组创建</a></li>
  <li><a href="#二矩阵操作" id="toc-二矩阵操作" class="nav-link" data-scroll-target="#二矩阵操作">二、矩阵操作</a></li>
  </ul></li>
  <li><a href="#线性代数" id="toc-线性代数" class="nav-link" data-scroll-target="#线性代数">线性代数</a>
  <ul class="collapse">
  <li><a href="#一矩阵求逆" id="toc-一矩阵求逆" class="nav-link" data-scroll-target="#一矩阵求逆">一、矩阵求逆</a></li>
  </ul></li>
  <li><a href="#逻辑运算" id="toc-逻辑运算" class="nav-link" data-scroll-target="#逻辑运算">逻辑运算</a>
  <ul class="collapse">
  <li><a href="#一比较运算符" id="toc-一比较运算符" class="nav-link" data-scroll-target="#一比较运算符">一、比较运算符</a></li>
  </ul></li>
  <li><a href="#优化及求根" id="toc-优化及求根" class="nav-link" data-scroll-target="#优化及求根">优化及求根</a>
  <ul class="collapse">
  <li><a href="#一一维优化" id="toc-一一维优化" class="nav-link" data-scroll-target="#一一维优化">一、一维优化</a></li>
  </ul></li>
  <li><a href="#程序设计" id="toc-程序设计" class="nav-link" data-scroll-target="#程序设计">程序设计</a>
  <ul class="collapse">
  <li><a href="#一控制结构" id="toc-一控制结构" class="nav-link" data-scroll-target="#一控制结构">一、控制结构</a></li>
  </ul></li>
  <li><a href="#输入输出" id="toc-输入输出" class="nav-link" data-scroll-target="#输入输出">输入输出</a>
  <ul class="collapse">
  <li><a href="#一显示对象" id="toc-一显示对象" class="nav-link" data-scroll-target="#一显示对象">一、显示对象</a></li>
  </ul></li>
  <li><a href="#文件操作" id="toc-文件操作" class="nav-link" data-scroll-target="#文件操作">文件操作</a>
  <ul class="collapse">
  <li><a href="#一文件执行" id="toc-一文件执行" class="nav-link" data-scroll-target="#一文件执行">一、文件执行</a></li>
  </ul></li>
  <li><a href="#网络浏览" id="toc-网络浏览" class="nav-link" data-scroll-target="#网络浏览">网络浏览</a>
  <ul class="collapse">
  <li><a href="#一浏览网页" id="toc-一浏览网页" class="nav-link" data-scroll-target="#一浏览网页">一、浏览网页</a></li>
  </ul></li>
  <li><a href="#文件操作-1" id="toc-文件操作-1" class="nav-link" data-scroll-target="#文件操作-1">文件操作</a>
  <ul class="collapse">
  <li><a href="#一文件创建" id="toc-一文件创建" class="nav-link" data-scroll-target="#一文件创建">一、文件创建</a></li>
  </ul></li>
  <li><a href="#数学相关计算-1" id="toc-数学相关计算-1" class="nav-link" data-scroll-target="#数学相关计算-1">数学相关计算</a>
  <ul class="collapse">
  <li><a href="#三数学函数" id="toc-三数学函数" class="nav-link" data-scroll-target="#三数学函数">三、数学函数</a></li>
  <li><a href="#四数组" id="toc-四数组" class="nav-link" data-scroll-target="#四数组">四、数组</a></li>
  <li><a href="#五线性代数" id="toc-五线性代数" class="nav-link" data-scroll-target="#五线性代数">五、线性代数</a></li>
  </ul></li>
  <li><a href="#程序设计-1" id="toc-程序设计-1" class="nav-link" data-scroll-target="#程序设计-1">程序设计</a>
  <ul class="collapse">
  <li><a href="#二函数定义" id="toc-二函数定义" class="nav-link" data-scroll-target="#二函数定义">二、函数定义</a></li>
  <li><a href="#三循环控制" id="toc-三循环控制" class="nav-link" data-scroll-target="#三循环控制">三、循环控制</a></li>
  <li><a href="#四函数应用" id="toc-四函数应用" class="nav-link" data-scroll-target="#四函数应用">四、函数应用</a></li>
  </ul></li>
  <li><a href="#统计分析" id="toc-统计分析" class="nav-link" data-scroll-target="#统计分析">统计分析</a>
  <ul class="collapse">
  <li><a href="#一统计分布" id="toc-一统计分布" class="nav-link" data-scroll-target="#一统计分布">一、统计分布</a></li>
  <li><a href="#二简单统计量" id="toc-二简单统计量" class="nav-link" data-scroll-target="#二简单统计量">二、简单统计量</a></li>
  <li><a href="#三统计检验" id="toc-三统计检验" class="nav-link" data-scroll-target="#三统计检验">三、统计检验</a></li>
  </ul></li>
  <li><a href="#文件操作-2" id="toc-文件操作-2" class="nav-link" data-scroll-target="#文件操作-2">文件操作</a>
  <ul class="collapse">
  <li><a href="#二网络浏览" id="toc-二网络浏览" class="nav-link" data-scroll-target="#二网络浏览">二、网络浏览</a></li>
  <li><a href="#三文件操作" id="toc-三文件操作" class="nav-link" data-scroll-target="#三文件操作">三、文件操作</a></li>
  </ul></li>
  <li><a href="#输入输出-1" id="toc-输入输出-1" class="nav-link" data-scroll-target="#输入输出-1">输入输出</a>
  <ul class="collapse">
  <li><a href="#二保存对象" id="toc-二保存对象" class="nav-link" data-scroll-target="#二保存对象">二、保存对象</a></li>
  <li><a href="#三读取数据" id="toc-三读取数据" class="nav-link" data-scroll-target="#三读取数据">三、读取数据</a></li>
  </ul></li>
  <li><a href="#工作环境" id="toc-工作环境" class="nav-link" data-scroll-target="#工作环境">工作环境</a>
  <ul class="collapse">
  <li><a href="#一显示对象列表" id="toc-一显示对象列表" class="nav-link" data-scroll-target="#一显示对象列表">一、显示对象列表</a></li>
  <li><a href="#二退出系统" id="toc-二退出系统" class="nav-link" data-scroll-target="#二退出系统">二、退出系统</a></li>
  </ul></li>
  <li><a href="#统计模型" id="toc-统计模型" class="nav-link" data-scroll-target="#统计模型">统计模型</a>
  <ul class="collapse">
  <li><a href="#一线性模型" id="toc-一线性模型" class="nav-link" data-scroll-target="#一线性模型">一、线性模型</a></li>
  <li><a href="#二广义线性模型" id="toc-二广义线性模型" class="nav-link" data-scroll-target="#二广义线性模型">二、广义线性模型</a></li>
  </ul></li>
  <li><a href="#数学相关计算-2" id="toc-数学相关计算-2" class="nav-link" data-scroll-target="#数学相关计算-2">数学相关计算</a>
  <ul class="collapse">
  <li><a href="#六逻辑运算" id="toc-六逻辑运算" class="nav-link" data-scroll-target="#六逻辑运算">六、逻辑运算</a></li>
  </ul></li>
  <li><a href="#程序设计-2" id="toc-程序设计-2" class="nav-link" data-scroll-target="#程序设计-2">程序设计</a>
  <ul class="collapse">
  <li><a href="#一控制结构-1" id="toc-一控制结构-1" class="nav-link" data-scroll-target="#一控制结构-1">一、控制结构</a></li>
  </ul></li>
  <li><a href="#程序设计-3" id="toc-程序设计-3" class="nav-link" data-scroll-target="#程序设计-3">程序设计</a>
  <ul class="collapse">
  <li><a href="#二函数方面" id="toc-二函数方面" class="nav-link" data-scroll-target="#二函数方面">二、函数方面</a></li>
  </ul></li>
  <li><a href="#输入输出-2" id="toc-输入输出-2" class="nav-link" data-scroll-target="#输入输出-2">输入输出</a>
  <ul class="collapse">
  <li><a href="#三显示对象" id="toc-三显示对象" class="nav-link" data-scroll-target="#三显示对象">三、显示对象</a></li>
  <li><a href="#四输出到文件" id="toc-四输出到文件" class="nav-link" data-scroll-target="#四输出到文件">四、输出到文件</a></li>
  <li><a href="#五保存和读取对象" id="toc-五保存和读取对象" class="nav-link" data-scroll-target="#五保存和读取对象">五、保存和读取对象</a></li>
  <li><a href="#六读取数据" id="toc-六读取数据" class="nav-link" data-scroll-target="#六读取数据">六、读取数据</a></li>
  </ul></li>
  <li><a href="#文件操作-3" id="toc-文件操作-3" class="nav-link" data-scroll-target="#文件操作-3">文件操作</a>
  <ul class="collapse">
  <li><a href="#三文件操作-1" id="toc-三文件操作-1" class="nav-link" data-scroll-target="#三文件操作-1">三、文件操作</a></li>
  </ul></li>
  <li><a href="#统计分析-1" id="toc-统计分析-1" class="nav-link" data-scroll-target="#统计分析-1">统计分析</a>
  <ul class="collapse">
  <li><a href="#四多元分析" id="toc-四多元分析" class="nav-link" data-scroll-target="#四多元分析">四、多元分析</a></li>
  <li><a href="#五时间序列" id="toc-五时间序列" class="nav-link" data-scroll-target="#五时间序列">五、时间序列</a></li>
  <li><a href="#六统计模型" id="toc-六统计模型" class="nav-link" data-scroll-target="#六统计模型">六、统计模型</a></li>
  </ul></li>
  <li><a href="#工作环境-1" id="toc-工作环境-1" class="nav-link" data-scroll-target="#工作环境-1">工作环境</a>
  <ul class="collapse">
  <li><a href="#三帮助功能" id="toc-三帮助功能" class="nav-link" data-scroll-target="#三帮助功能">三、帮助功能</a></li>
  <li><a href="#四数据集" id="toc-四数据集" class="nav-link" data-scroll-target="#四数据集">四、数据集</a></li>
  </ul></li>
  <li><a href="#统计分析-2" id="toc-统计分析-2" class="nav-link" data-scroll-target="#统计分析-2">统计分析</a>
  <ul class="collapse">
  <li><a href="#四多元分析续" id="toc-四多元分析续" class="nav-link" data-scroll-target="#四多元分析续">四、多元分析（续）</a></li>
  </ul></li>
  <li><a href="#时间序列分析" id="toc-时间序列分析" class="nav-link" data-scroll-target="#时间序列分析">时间序列分析</a>
  <ul class="collapse">
  <li><a href="#五时间序列续" id="toc-五时间序列续" class="nav-link" data-scroll-target="#五时间序列续">五、时间序列（续）</a></li>
  </ul></li>
  <li><a href="#文件操作-4" id="toc-文件操作-4" class="nav-link" data-scroll-target="#文件操作-4">文件操作</a>
  <ul class="collapse">
  <li><a href="#四清理创建的演示文件" id="toc-四清理创建的演示文件" class="nav-link" data-scroll-target="#四清理创建的演示文件">四、清理创建的演示文件</a></li>
  </ul></li>
  </ul>
</nav>
</div>
<main class="content" id="quarto-document-content">

<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">R语言常用函数手册</h1>
</div>



<div class="quarto-title-meta">

    
    <div>
    <div class="quarto-title-meta-heading">Published</div>
    <div class="quarto-title-meta-contents">
      <p class="date">February 22, 2025</p>
    </div>
  </div>
  
    
  </div>
  


</header>


<section id="数据结构" class="level2">
<h2 class="anchored" data-anchor-id="数据结构">数据结构</h2>
<section id="一数据管理" class="level3">
<h3 class="anchored" data-anchor-id="一数据管理">一、数据管理</h3>
<section id="vector向量" class="level4">
<h4 class="anchored" data-anchor-id="vector向量">Vector（向量）</h4>
<ul>
<li><p><strong><code>vector</code></strong>：创建向量。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>v <span class="ot">&lt;-</span> <span class="fu">vector</span>(<span class="st">"numeric"</span>, <span class="at">length =</span> <span class="dv">5</span>)  <span class="co"># 创建一个长度为5的数值型向量</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>v</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0 0 0 0 0</code></pre>
</div>
</div></li>
<li><p><strong><code>numeric</code></strong>：数值型向量。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>num_vec <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)  <span class="co"># 创建数值型向量</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>num_vec</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3 4 5</code></pre>
</div>
</div></li>
<li><p><strong><code>logical</code></strong>：逻辑型向量。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>log_vec <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>, <span class="cn">TRUE</span>)  <span class="co"># 创建逻辑型向量</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>log_vec</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  TRUE FALSE  TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>character</code></strong>：字符型向量。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>char_vec <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>)  <span class="co"># 创建字符型向量</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>char_vec</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "a" "b" "c"</code></pre>
</div>
</div></li>
<li><p><strong><code>list</code></strong>：列表。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>my_list <span class="ot">&lt;-</span> <span class="fu">list</span>(<span class="at">a =</span> <span class="dv">1</span>, <span class="at">b =</span> <span class="st">"text"</span>, <span class="at">c =</span> <span class="cn">TRUE</span>)  <span class="co"># 创建列表</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>my_list</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$a
[1] 1

$b
[1] "text"

$c
[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>data.frame</code></strong>：数据框。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(<span class="at">name =</span> <span class="fu">c</span>(<span class="st">"Alice"</span>, <span class="st">"Bob"</span>), <span class="at">age =</span> <span class="fu">c</span>(<span class="dv">25</span>, <span class="dv">30</span>))  <span class="co"># 创建数据框</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>df</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   name age
1 Alice  25
2   Bob  30</code></pre>
</div>
</div></li>
<li><p><strong><code>c</code></strong>：连接为向量或列表。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>combined <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>)  <span class="co"># 连接为向量</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>combined</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3 4</code></pre>
</div>
</div></li>
<li><p><strong><code>length</code></strong>：求长度。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(num_vec)  <span class="co"># 输出向量长度</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
</div></li>
<li><p><strong><code>subset</code></strong>：求子集。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">subset</span>(df, age <span class="sc">&gt;</span> <span class="dv">25</span>)  <span class="co"># 提取年龄大于25的子集</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  name age
2  Bob  30</code></pre>
</div>
</div></li>
<li><p><strong><code>seq</code></strong>：等差序列。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">seq</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="at">by =</span> <span class="dv">2</span>)  <span class="co"># 创建等差序列</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 3 5 7 9</code></pre>
</div>
</div></li>
<li><p><strong><code>rep</code></strong>：重复。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rep</span>(<span class="st">"R"</span>, <span class="at">times =</span> <span class="dv">3</span>)  <span class="co"># 重复字符串"R"三次</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "R" "R" "R"</code></pre>
</div>
</div></li>
<li><p><strong><code>NA</code></strong>：缺失值。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>na_vec <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="cn">NA</span>, <span class="dv">3</span>)  <span class="co"># 包含缺失值的向量</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a>na_vec</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  1 NA  3</code></pre>
</div>
</div></li>
<li><p><strong><code>NULL</code></strong>：空对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>null_obj <span class="ot">&lt;-</span> <span class="cn">NULL</span>  <span class="co"># 创建空对象</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>null_obj</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
</div></li>
<li><p><strong><code>sort</code></strong>：排序。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sort</span>(num_vec)  <span class="co"># 对数值型向量排序</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3 4 5</code></pre>
</div>
</div></li>
<li><p><strong><code>unlist</code></strong>：展平列表。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">unlist</span>(my_list)  <span class="co"># 将列表展平为向量</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     a      b      c 
   "1" "text" "TRUE" </code></pre>
</div>
</div></li>
</ul>
</section>
</section>
</section>
<section id="字符串处理" class="level2">
<h2 class="anchored" data-anchor-id="字符串处理">字符串处理</h2>
<section id="一字符型向量" class="level3">
<h3 class="anchored" data-anchor-id="一字符型向量">一、字符型向量</h3>
<ul>
<li><p><strong><code>character</code></strong>：字符型向量。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>char_vec <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"hello"</span>, <span class="st">"world"</span>)  <span class="co"># 创建字符型向量</span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>char_vec</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "hello" "world"</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="二字符数" class="level3">
<h3 class="anchored" data-anchor-id="二字符数">二、字符数</h3>
<ul>
<li><p><strong><code>nchar</code></strong>：计算字符数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">nchar</span>(char_vec)  <span class="co"># 计算每个字符串的字符数</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5 5</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="三取子串" class="level3">
<h3 class="anchored" data-anchor-id="三取子串">三、取子串</h3>
<ul>
<li><p><strong><code>substr</code></strong>：提取子串。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">substr</span>(<span class="st">"hello"</span>, <span class="dv">1</span>, <span class="dv">3</span>)  <span class="co"># 提取从第1个到第3个字符</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "hel"</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="四连接或拆分" class="level3">
<h3 class="anchored" data-anchor-id="四连接或拆分">四、连接或拆分</h3>
<ul>
<li><p><strong><code>paste</code></strong>：连接字符串。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">paste</span>(<span class="st">"hello"</span>, <span class="st">"world"</span>)  <span class="co"># 连接字符串</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "hello world"</code></pre>
</div>
</div></li>
<li><p><strong><code>strsplit</code></strong>：拆分字符串。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">strsplit</span>(<span class="st">"hello world"</span>, <span class="st">" "</span>)[[<span class="dv">1</span>]]  <span class="co"># 按空格拆分字符串</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "hello" "world"</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="复数" class="level2">
<h2 class="anchored" data-anchor-id="复数">复数</h2>
<section id="一复数函数" class="level3">
<h3 class="anchored" data-anchor-id="一复数函数">一、复数函数</h3>
<ul>
<li><p><strong><code>complex</code></strong>：创建复数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>z <span class="ot">&lt;-</span> <span class="fu">complex</span>(<span class="at">real =</span> <span class="dv">1</span>, <span class="at">imaginary =</span> <span class="dv">2</span>)  <span class="co"># 创建复数1+2i</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a>z</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1+2i</code></pre>
</div>
</div></li>
<li><p><strong><code>Re</code></strong>：提取实部。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Re</span>(z)  <span class="co"># 提取实部</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div></li>
<li><p><strong><code>Im</code></strong>：提取虚部。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Im</span>(z)  <span class="co"># 提取虚部</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="因子" class="level2">
<h2 class="anchored" data-anchor-id="因子">因子</h2>
<section id="一因子" class="level3">
<h3 class="anchored" data-anchor-id="一因子">一、因子</h3>
<ul>
<li><p><strong><code>factor</code></strong>：创建因子。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>f <span class="ot">&lt;-</span> <span class="fu">factor</span>(<span class="fu">c</span>(<span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"A"</span>, <span class="st">"C"</span>))  <span class="co"># 创建因子</span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a>f</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] A B A C
Levels: A B C</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="二因子的水平" class="level3">
<h3 class="anchored" data-anchor-id="二因子的水平">二、因子的水平</h3>
<ul>
<li><p><strong><code>levels</code></strong>：获取因子的水平。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">levels</span>(f)  <span class="co"># 获取因子的水平</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "A" "B" "C"</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="数学相关计算" class="level2">
<h2 class="anchored" data-anchor-id="数学相关计算">数学相关计算</h2>
<section id="一基本计算" class="level3">
<h3 class="anchored" data-anchor-id="一基本计算">一、基本计算</h3>
<ul>
<li><p><strong><code>+</code>, <code>-</code>, <code>*</code>, <code>/</code></strong>：四则运算。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="dv">1</span> <span class="sc">+</span> <span class="dv">2</span>  <span class="co"># 加法</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div></li>
<li><p><strong><code>max</code>, <code>min</code></strong>：最大值和最小值。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="fu">max</span>(num_vec)  <span class="co"># 获取最大值</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="fu">min</span>(num_vec)  <span class="co"># 获取最小值</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="二数学函数" class="level3">
<h3 class="anchored" data-anchor-id="二数学函数">二、数学函数</h3>
<ul>
<li><p><strong><code>abs</code></strong>：绝对值。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="fu">abs</span>(<span class="sc">-</span><span class="dv">5</span>)  <span class="co"># 计算绝对值</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
</div></li>
<li><p><strong><code>sqrt</code></strong>：平方根。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb59"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sqrt</span>(<span class="dv">16</span>)  <span class="co"># 计算平方根</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="数组" class="level2">
<h2 class="anchored" data-anchor-id="数组">数组</h2>
<section id="一数组创建" class="level3">
<h3 class="anchored" data-anchor-id="一数组创建">一、数组创建</h3>
<ul>
<li><p><strong><code>array</code></strong>：创建数组。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb61"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a>arr <span class="ot">&lt;-</span> <span class="fu">array</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">12</span>, <span class="at">dim =</span> <span class="fu">c</span>(<span class="dv">3</span>, <span class="dv">4</span>))  <span class="co"># 创建3x4数组</span></span>
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a>arr</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="二矩阵操作" class="level3">
<h3 class="anchored" data-anchor-id="二矩阵操作">二、矩阵操作</h3>
<ul>
<li><p><strong><code>matrix</code></strong>：生成矩阵。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb63"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)  <span class="co"># 设置随机种子以确保结果可重复</span></span>
<span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a>random_mat <span class="ot">&lt;-</span> <span class="fu">matrix</span>(<span class="fu">runif</span>(<span class="dv">9</span>), <span class="at">nrow =</span> <span class="dv">3</span>)</span>
<span id="cb63-3"><a href="#cb63-3" aria-hidden="true" tabindex="-1"></a>random_mat</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>          [,1]      [,2]      [,3]
[1,] 0.2875775 0.8830174 0.5281055
[2,] 0.7883051 0.9404673 0.8924190
[3,] 0.4089769 0.0455565 0.5514350</code></pre>
</div>
<div class="sourceCode cell-code" id="cb65"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="fu">det</span>(random_mat)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.1082756</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="线性代数" class="level2">
<h2 class="anchored" data-anchor-id="线性代数">线性代数</h2>
<section id="一矩阵求逆" class="level3">
<h3 class="anchored" data-anchor-id="一矩阵求逆">一、矩阵求逆</h3>
<ul>
<li><p><strong><code>solve</code></strong>：求矩阵的逆。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb67"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>inv_mat <span class="ot">&lt;-</span> <span class="fu">solve</span>(random_mat)  <span class="co"># 求矩阵的逆</span></span>
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a>inv_mat</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>          [,1]       [,2]      [,3]
[1,] -4.414210  4.2749079 -2.690871
[2,]  0.643915  0.5301532 -1.474650
[3,]  3.220643 -3.2143236  3.930988</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="逻辑运算" class="level2">
<h2 class="anchored" data-anchor-id="逻辑运算">逻辑运算</h2>
<section id="一比较运算符" class="level3">
<h3 class="anchored" data-anchor-id="一比较运算符">一、比较运算符</h3>
<ul>
<li><p><strong><code>&lt;</code>, <code>&gt;</code>, <code>==</code></strong>：比较运算。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb69"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a><span class="dv">1</span> <span class="sc">&lt;</span> <span class="dv">2</span>  <span class="co"># 比较大小</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="优化及求根" class="level2">
<h2 class="anchored" data-anchor-id="优化及求根">优化及求根</h2>
<section id="一一维优化" class="level3">
<h3 class="anchored" data-anchor-id="一一维优化">一、一维优化</h3>
<ul>
<li><p><strong><code>optimize</code></strong>：一维优化。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb71"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a><span class="fu">optimize</span>(<span class="cf">function</span>(x) x<span class="sc">^</span><span class="dv">2</span>, <span class="at">interval =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">10</span>, <span class="dv">10</span>))  <span class="co"># 最小化x^2</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$minimum
[1] 0

$objective
[1] 0</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="程序设计" class="level2">
<h2 class="anchored" data-anchor-id="程序设计">程序设计</h2>
<section id="一控制结构" class="level3">
<h3 class="anchored" data-anchor-id="一控制结构">一、控制结构</h3>
<ul>
<li><p><strong><code>if</code></strong>：条件判断。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb73"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="dv">1</span> <span class="sc">&lt;</span> <span class="dv">2</span>) {</span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(<span class="st">"1 is less than 2"</span>)</span>
<span id="cb73-3"><a href="#cb73-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 is less than 2"</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="输入输出" class="level2">
<h2 class="anchored" data-anchor-id="输入输出">输入输出</h2>
<section id="一显示对象" class="level3">
<h3 class="anchored" data-anchor-id="一显示对象">一、显示对象</h3>
<ul>
<li><p><strong><code>print</code></strong>：打印对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb75"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(num_vec)  <span class="co"># 打印向量</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3 4 5</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="文件操作" class="level2">
<h2 class="anchored" data-anchor-id="文件操作">文件操作</h2>
<section id="一文件执行" class="level3">
<h3 class="anchored" data-anchor-id="一文件执行">一、文件执行</h3>
<ul>
<li><p><strong><code>system</code></strong>：调用系统命令。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb77"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span>(<span class="st">'ls'</span>)  <span class="co"># 使用pandoc转换文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
</section>
</section>
<section id="网络浏览" class="level2">
<h2 class="anchored" data-anchor-id="网络浏览">网络浏览</h2>
<section id="一浏览网页" class="level3">
<h3 class="anchored" data-anchor-id="一浏览网页">一、浏览网页</h3>
<ul>
<li><p><strong><code>browseURL</code></strong>：打开网页。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb78"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a><span class="co"># browseURL("https://www.r-project.org")  # 打开R官方网站</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
</section>
</section>
<section id="文件操作-1" class="level2">
<h2 class="anchored" data-anchor-id="文件操作-1">文件操作</h2>
<section id="一文件创建" class="level3">
<h3 class="anchored" data-anchor-id="一文件创建">一、文件创建</h3>
<ul>
<li><p><strong><code>file.create</code></strong>：创建文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb79"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.create</span>(<span class="st">"example.txt"</span>)  <span class="co"># 创建文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
</ul>
</section>
</section>
<section id="数学相关计算-1" class="level2">
<h2 class="anchored" data-anchor-id="数学相关计算-1">数学相关计算</h2>
<section id="三数学函数" class="level3">
<h3 class="anchored" data-anchor-id="三数学函数">三、数学函数</h3>
<ul>
<li><p><strong><code>log</code></strong>：自然对数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb81"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a><span class="fu">log</span>(<span class="dv">10</span>)  <span class="co"># 计算自然对数</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.302585</code></pre>
</div>
</div></li>
<li><p><strong><code>exp</code></strong>：指数函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb83"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a><span class="fu">exp</span>(<span class="dv">1</span>)  <span class="co"># 计算e的1次方</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.718282</code></pre>
</div>
</div></li>
<li><p><strong><code>sin</code></strong>：正弦函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb85"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sin</span>(pi <span class="sc">/</span> <span class="dv">2</span>)  <span class="co"># 计算sin(90°)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div></li>
<li><p><strong><code>cos</code></strong>：余弦函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb87"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb87-1"><a href="#cb87-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cos</span>(pi)  <span class="co"># 计算cos(180°)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -1</code></pre>
</div>
</div></li>
<li><p><strong><code>tan</code></strong>：正切函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb89"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb89-1"><a href="#cb89-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tan</span>(pi <span class="sc">/</span> <span class="dv">4</span>)  <span class="co"># 计算tan(45°)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="四数组" class="level3">
<h3 class="anchored" data-anchor-id="四数组">四、数组</h3>
<ul>
<li><p><strong><code>dim</code></strong>：获取数组的维度。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb91"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb91-1"><a href="#cb91-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dim</span>(arr)  <span class="co"># 获取数组的维度</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3 4</code></pre>
</div>
</div></li>
<li><p><strong><code>nrow</code></strong>：获取矩阵的行数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb93"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb93-1"><a href="#cb93-1" aria-hidden="true" tabindex="-1"></a>mat <span class="ot">=</span> random_mat</span>
<span id="cb93-2"><a href="#cb93-2" aria-hidden="true" tabindex="-1"></a><span class="fu">nrow</span>(mat)  <span class="co"># 获取矩阵的行数</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div></li>
<li><p><strong><code>ncol</code></strong>：获取矩阵的列数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb95"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb95-1"><a href="#cb95-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ncol</span>(mat)  <span class="co"># 获取矩阵的列数</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div></li>
<li><p><strong><code>t</code></strong>：矩阵转置。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb97"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb97-1"><a href="#cb97-1" aria-hidden="true" tabindex="-1"></a><span class="fu">t</span>(mat)  <span class="co"># 转置矩阵</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>          [,1]      [,2]      [,3]
[1,] 0.2875775 0.7883051 0.4089769
[2,] 0.8830174 0.9404673 0.0455565
[3,] 0.5281055 0.8924190 0.5514350</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="五线性代数" class="level3">
<h3 class="anchored" data-anchor-id="五线性代数">五、线性代数</h3>
<ul>
<li><p><strong><code>eigen</code></strong>：特征值分解。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb99"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb99-1"><a href="#cb99-1" aria-hidden="true" tabindex="-1"></a><span class="fu">eigen</span>(mat)  <span class="co"># 计算矩阵的特征值和特征向量</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>eigen() decomposition
$values
[1]  1.7469081  0.2657783 -0.2332066

$vectors
          [,1]       [,2]       [,3]
[1,] 0.5624253 -0.4802320  0.8646765
[2,] 0.7962783 -0.4414539 -0.2490700
[3,] 0.2227525  0.7579549 -0.4362323</code></pre>
</div>
</div></li>
<li><p><strong><code>svd</code></strong>：奇异值分解。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb101"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb101-1"><a href="#cb101-1" aria-hidden="true" tabindex="-1"></a><span class="fu">svd</span>(mat)  <span class="co"># 计算矩阵的奇异值分解</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$d
[1] 1.9099423 0.5066041 0.1119030

$u
           [,1]       [,2]       [,3]
[1,] -0.5355839  0.5934994  0.6007565
[2,] -0.7931368 -0.1092486 -0.5991651
[3,] -0.2899723 -0.7973852  0.5292380

$v
           [,1]       [,2]        [,3]
[1,] -0.4700915 -0.4768147 -0.74273931
[2,] -0.6450767  0.7599614 -0.07959146
[3,] -0.6024036 -0.4417085  0.66483345</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="程序设计-1" class="level2">
<h2 class="anchored" data-anchor-id="程序设计-1">程序设计</h2>
<section id="二函数定义" class="level3">
<h3 class="anchored" data-anchor-id="二函数定义">二、函数定义</h3>
<ul>
<li><p><strong><code>function</code></strong>：定义函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb103"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb103-1"><a href="#cb103-1" aria-hidden="true" tabindex="-1"></a>my_function <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
<span id="cb103-2"><a href="#cb103-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">return</span>(x<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb103-3"><a href="#cb103-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb103-4"><a href="#cb103-4" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>(<span class="dv">3</span>)  <span class="co"># 调用函数，输出9</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 9</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="三循环控制" class="level3">
<h3 class="anchored" data-anchor-id="三循环控制">三、循环控制</h3>
<ul>
<li><p><strong><code>for</code></strong>：循环。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb105"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb105-1"><a href="#cb105-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>) {</span>
<span id="cb105-2"><a href="#cb105-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(i)</span>
<span id="cb105-3"><a href="#cb105-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3
[1] 4
[1] 5</code></pre>
</div>
</div></li>
<li><p><strong><code>while</code></strong>：条件循环。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb107"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb107-1"><a href="#cb107-1" aria-hidden="true" tabindex="-1"></a>i <span class="ot">&lt;-</span> <span class="dv">1</span></span>
<span id="cb107-2"><a href="#cb107-2" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> (i <span class="sc">&lt;=</span> <span class="dv">5</span>) {</span>
<span id="cb107-3"><a href="#cb107-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(i)</span>
<span id="cb107-4"><a href="#cb107-4" aria-hidden="true" tabindex="-1"></a>    i <span class="ot">&lt;-</span> i <span class="sc">+</span> <span class="dv">1</span></span>
<span id="cb107-5"><a href="#cb107-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3
[1] 4
[1] 5</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="四函数应用" class="level3">
<h3 class="anchored" data-anchor-id="四函数应用">四、函数应用</h3>
<ul>
<li><p><strong><code>apply</code></strong>：对数组的某些维应用函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb109"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb109-1"><a href="#cb109-1" aria-hidden="true" tabindex="-1"></a><span class="fu">apply</span>(mat, <span class="dv">2</span>, sum)  <span class="co"># 对矩阵的每一列求和</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.484860 1.869041 1.971960</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="统计分析" class="level2">
<h2 class="anchored" data-anchor-id="统计分析">统计分析</h2>
<section id="一统计分布" class="level3">
<h3 class="anchored" data-anchor-id="一统计分布">一、统计分布</h3>
<ul>
<li><p><strong><code>dnorm</code></strong>：正态分布密度函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb111"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb111-1"><a href="#cb111-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dnorm</span>(<span class="dv">0</span>, <span class="at">mean =</span> <span class="dv">0</span>, <span class="at">sd =</span> <span class="dv">1</span>)  <span class="co"># 计算标准正态分布的密度</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.3989423</code></pre>
</div>
</div></li>
<li><p><strong><code>rnorm</code></strong>：生成正态分布随机数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb113"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb113-1"><a href="#cb113-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rnorm</span>(<span class="dv">5</span>, <span class="at">mean =</span> <span class="dv">0</span>, <span class="at">sd =</span> <span class="dv">1</span>)  <span class="co"># 生成5个正态分布随机数</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.1089660 -0.1172420  0.1830826  1.2805549 -1.7272706</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="二简单统计量" class="level3">
<h3 class="anchored" data-anchor-id="二简单统计量">二、简单统计量</h3>
<ul>
<li><p><strong><code>mean</code></strong>：计算均值。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb115"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb115-1"><a href="#cb115-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(num_vec)  <span class="co"># 计算向量的均值</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div></li>
<li><p><strong><code>var</code></strong>：计算方差。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb117"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb117-1"><a href="#cb117-1" aria-hidden="true" tabindex="-1"></a><span class="fu">var</span>(num_vec)  <span class="co"># 计算向量的方差</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.5</code></pre>
</div>
</div></li>
<li><p><strong><code>sd</code></strong>：计算标准差。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb119"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb119-1"><a href="#cb119-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sd</span>(num_vec)  <span class="co"># 计算向量的标准差</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.581139</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="三统计检验" class="level3">
<h3 class="anchored" data-anchor-id="三统计检验">三、统计检验</h3>
<ul>
<li><p><strong><code>t.test</code></strong>：t检验。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb121"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb121-1"><a href="#cb121-1" aria-hidden="true" tabindex="-1"></a><span class="fu">t.test</span>(num_vec, <span class="at">mu =</span> <span class="dv">3</span>)  <span class="co"># 单样本t检验</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    One Sample t-test

data:  num_vec
t = 0, df = 4, p-value = 1
alternative hypothesis: true mean is not equal to 3
95 percent confidence interval:
 1.036757 4.963243
sample estimates:
mean of x 
        3 </code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="文件操作-2" class="level2">
<h2 class="anchored" data-anchor-id="文件操作-2">文件操作</h2>
<section id="二网络浏览" class="level3">
<h3 class="anchored" data-anchor-id="二网络浏览">二、网络浏览</h3>
<ul>
<li><p><strong><code>download.file</code></strong>：下载网络文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb123"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb123-1"><a href="#cb123-1" aria-hidden="true" tabindex="-1"></a><span class="fu">download.file</span>(<span class="st">"https://pixnio.com/free-images/2023/07/11/2023-07-11-12-25-45.jpg"</span>, <span class="at">destfile =</span> <span class="st">"file.jpg"</span>) </span>
<span id="cb123-2"><a href="#cb123-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 下载文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
<p>[<img src="file.jpg" class="img-fluid">](<a href="https://pixnio.com/zh/media/zh-2916062">照片</a> 者 <a href="https://pixnio.com/zh/author/djovan">djovan</a> 从 <a href="https://pixnio.com/zh/">Pixnio</a>)</p>
</section>
<section id="三文件操作" class="level3">
<h3 class="anchored" data-anchor-id="三文件操作">三、文件操作</h3>
<ul>
<li><p><strong><code>dir.create</code></strong>：创建文件夹。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb124"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb124-1"><a href="#cb124-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dir.create</span>(<span class="st">"new_folder"</span>)  <span class="co"># 创建文件夹</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>list.files</code></strong>：列出文件夹中的文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb125"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb125-1"><a href="#cb125-1" aria-hidden="true" tabindex="-1"></a><span class="fu">list.files</span>(<span class="st">"new_folder"</span>)  <span class="co"># 列出文件夹中的文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>character(0)</code></pre>
</div>
</div></li>
<li><p><strong><code>file.exists</code></strong>：检查文件是否存在。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb127"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb127-1"><a href="#cb127-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.exists</span>(<span class="st">"example.txt"</span>)  <span class="co"># 检查文件是否存在</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>file.remove</code></strong>：删除文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb129"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb129-1"><a href="#cb129-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"example.txt"</span>)  <span class="co"># 删除文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>file.copy</code></strong>：复制文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb131"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb131-1"><a href="#cb131-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.create</span>(<span class="st">"file1.txt"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb133"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb133-1"><a href="#cb133-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.copy</span>(<span class="st">"file1.txt"</span>, <span class="st">"file2.txt"</span>)  <span class="co"># 复制文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>zip</code></strong>：压缩文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb135"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb135-1"><a href="#cb135-1" aria-hidden="true" tabindex="-1"></a><span class="fu">zip</span>(<span class="st">"archive.zip"</span>, <span class="at">files =</span> <span class="fu">c</span>(<span class="st">"file1.txt"</span>, <span class="st">"file2.txt"</span>))  <span class="co"># 压缩文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="输入输出-1" class="level2">
<h2 class="anchored" data-anchor-id="输入输出-1">输入输出</h2>
<section id="二保存对象" class="level3">
<h3 class="anchored" data-anchor-id="二保存对象">二、保存对象</h3>
<ul>
<li><p><strong><code>save</code></strong>：保存R对象到文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb136"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb136-1"><a href="#cb136-1" aria-hidden="true" tabindex="-1"></a><span class="fu">save</span>(num_vec, <span class="at">file =</span> <span class="st">"data.RData"</span>)  <span class="co"># 保存对象到文件</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>load</code></strong>：加载R对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb137"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb137-1"><a href="#cb137-1" aria-hidden="true" tabindex="-1"></a><span class="fu">load</span>(<span class="st">"data.RData"</span>)  <span class="co"># 加载保存的对象</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
</section>
<section id="三读取数据" class="level3">
<h3 class="anchored" data-anchor-id="三读取数据">三、读取数据</h3>
<ul>
<li><p><strong><code>read.table</code></strong>：从文本文件读取数据。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb138"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb138-1"><a href="#cb138-1" aria-hidden="true" tabindex="-1"></a><span class="fu">write.csv</span>(iris, <span class="st">"data.txt"</span>)</span>
<span id="cb138-2"><a href="#cb138-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.table</span>(<span class="st">"data.txt"</span>, <span class="at">header =</span> <span class="cn">TRUE</span>)  <span class="co"># 从文本文件读取数据</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="工作环境" class="level2">
<h2 class="anchored" data-anchor-id="工作环境">工作环境</h2>
<section id="一显示对象列表" class="level3">
<h3 class="anchored" data-anchor-id="一显示对象列表">一、显示对象列表</h3>
<ul>
<li><p><strong><code>ls</code></strong>：列出当前环境中的对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb139"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb139-1"><a href="#cb139-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ls</span>()  <span class="co"># 列出当前环境中的对象</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "arr"         "char_vec"    "combined"    "data"        "df"         
 [6] "f"           "i"           "inv_mat"     "log_vec"     "mat"        
[11] "my_function" "my_list"     "na_vec"      "null_obj"    "num_vec"    
[16] "random_mat"  "v"           "z"          </code></pre>
</div>
</div></li>
<li><p><strong><code>rm</code></strong>：删除对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb141"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb141-1"><a href="#cb141-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rm</span>(num_vec)  <span class="co"># 删除对象</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
</section>
<section id="二退出系统" class="level3">
<h3 class="anchored" data-anchor-id="二退出系统">二、退出系统</h3>
<ul>
<li><p><strong><code>q</code></strong>：退出R。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb142"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb142-1"><a href="#cb142-1" aria-hidden="true" tabindex="-1"></a><span class="co"># q()  # 退出R</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="统计模型" class="level2">
<h2 class="anchored" data-anchor-id="统计模型">统计模型</h2>
<section id="一线性模型" class="level3">
<h3 class="anchored" data-anchor-id="一线性模型">一、线性模型</h3>
<ul>
<li><p><strong><code>lm</code></strong>：线性模型。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb143"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb143-1"><a href="#cb143-1" aria-hidden="true" tabindex="-1"></a>model <span class="ot">&lt;-</span> <span class="fu">lm</span>(mpg <span class="sc">~</span> wt <span class="sc">+</span> hp, <span class="at">data =</span> mtcars)  <span class="co"># 拟合线性模型</span></span>
<span id="cb143-2"><a href="#cb143-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(model)  <span class="co"># 查看模型摘要</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = mpg ~ wt + hp, data = mtcars)

Residuals:
   Min     1Q Median     3Q    Max 
-3.941 -1.600 -0.182  1.050  5.854 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) 37.22727    1.59879  23.285  &lt; 2e-16 ***
wt          -3.87783    0.63273  -6.129 1.12e-06 ***
hp          -0.03177    0.00903  -3.519  0.00145 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.593 on 29 degrees of freedom
Multiple R-squared:  0.8268,    Adjusted R-squared:  0.8148 
F-statistic: 69.21 on 2 and 29 DF,  p-value: 9.109e-12</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="二广义线性模型" class="level3">
<h3 class="anchored" data-anchor-id="二广义线性模型">二、广义线性模型</h3>
<ul>
<li><p><strong><code>glm</code></strong>：广义线性模型。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb145"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb145-1"><a href="#cb145-1" aria-hidden="true" tabindex="-1"></a>glm_model <span class="ot">&lt;-</span> <span class="fu">glm</span>(mpg <span class="sc">~</span> wt <span class="sc">+</span> hp, <span class="at">data =</span> mtcars, <span class="at">family =</span> gaussian)  <span class="co"># 拟合广义线性模型</span></span>
<span id="cb145-2"><a href="#cb145-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(glm_model)  <span class="co"># 查看模型摘要</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = mpg ~ wt + hp, family = gaussian, data = mtcars)

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) 37.22727    1.59879  23.285  &lt; 2e-16 ***
wt          -3.87783    0.63273  -6.129 1.12e-06 ***
hp          -0.03177    0.00903  -3.519  0.00145 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 6.725785)

    Null deviance: 1126.05  on 31  degrees of freedom
Residual deviance:  195.05  on 29  degrees of freedom
AIC: 156.65

Number of Fisher Scoring iterations: 2</code></pre>
</div>
</div></li>
</ul>
</section>
</section>
<section id="数学相关计算-2" class="level2">
<h2 class="anchored" data-anchor-id="数学相关计算-2">数学相关计算</h2>
<section id="六逻辑运算" class="level3">
<h3 class="anchored" data-anchor-id="六逻辑运算">六、逻辑运算</h3>
<section id="比较运算符" class="level4">
<h4 class="anchored" data-anchor-id="比较运算符">比较运算符</h4>
<ul>
<li><p><strong><code>&lt;</code>、<code>&gt;</code>、<code>&lt;=</code>、<code>&gt;=</code>、<code>==</code>、<code>!=</code></strong>：比较运算符。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb147"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb147-1"><a href="#cb147-1" aria-hidden="true" tabindex="-1"></a><span class="dv">3</span> <span class="sc">&lt;</span> <span class="dv">5</span>  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb149"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb149-1"><a href="#cb149-1" aria-hidden="true" tabindex="-1"></a><span class="dv">5</span> <span class="sc">==</span> <span class="dv">5</span>  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb151"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb151-1"><a href="#cb151-1" aria-hidden="true" tabindex="-1"></a><span class="dv">3</span> <span class="sc">!=</span> <span class="dv">5</span>  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="逻辑运算符" class="level4">
<h4 class="anchored" data-anchor-id="逻辑运算符">逻辑运算符</h4>
<ul>
<li><p><strong><code>!</code></strong>：逻辑非。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb153"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb153-1"><a href="#cb153-1" aria-hidden="true" tabindex="-1"></a><span class="sc">!</span><span class="cn">TRUE</span>  <span class="co"># FALSE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div></li>
<li><p><strong><code>&amp;</code>、<code>&amp;&amp;</code></strong>：逻辑与。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb155"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb155-1"><a href="#cb155-1" aria-hidden="true" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">&amp;</span> <span class="cn">FALSE</span>  <span class="co"># FALSE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb157"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb157-1"><a href="#cb157-1" aria-hidden="true" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">&amp;&amp;</span> <span class="cn">FALSE</span>  <span class="co"># FALSE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div></li>
<li><p><strong><code>|</code>、<code>||</code></strong>：逻辑或。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb159"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb159-1"><a href="#cb159-1" aria-hidden="true" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">|</span> <span class="cn">FALSE</span>  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb161"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb161-1"><a href="#cb161-1" aria-hidden="true" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">||</span> <span class="cn">FALSE</span>  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>xor()</code></strong>：逻辑异或。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb163"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb163-1"><a href="#cb163-1" aria-hidden="true" tabindex="-1"></a><span class="fu">xor</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>)  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="其他逻辑函数" class="level4">
<h4 class="anchored" data-anchor-id="其他逻辑函数">其他逻辑函数</h4>
<ul>
<li><p><strong><code>logical</code></strong>：生成逻辑向量。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb165"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb165-1"><a href="#cb165-1" aria-hidden="true" tabindex="-1"></a><span class="fu">logical</span>(<span class="dv">3</span>)  <span class="co"># 创建长度为3的逻辑向量，默认值为FALSE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE FALSE FALSE</code></pre>
</div>
</div></li>
<li><p><strong><code>all</code></strong>：判断逻辑向量是否全部为<code>TRUE</code>。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb167"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb167-1"><a href="#cb167-1" aria-hidden="true" tabindex="-1"></a><span class="fu">all</span>(<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">TRUE</span>, <span class="cn">TRUE</span>))  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>any</code></strong>：判断逻辑向量中是否有<code>TRUE</code>。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb169"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb169-1"><a href="#cb169-1" aria-hidden="true" tabindex="-1"></a><span class="fu">any</span>(<span class="fu">c</span>(<span class="cn">FALSE</span>, <span class="cn">TRUE</span>, <span class="cn">FALSE</span>))  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>ifelse()</code></strong>：条件选择。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb171"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb171-1"><a href="#cb171-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ifelse</span>(<span class="dv">1</span> <span class="sc">&lt;</span> <span class="dv">2</span>, <span class="st">"Yes"</span>, <span class="st">"No"</span>)  <span class="co"># "Yes"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Yes"</code></pre>
</div>
</div></li>
<li><p><strong><code>match</code></strong>：查找元素的位置。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb173"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb173-1"><a href="#cb173-1" aria-hidden="true" tabindex="-1"></a><span class="fu">match</span>(<span class="st">"a"</span>, <span class="fu">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>))  <span class="co"># 1</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div></li>
<li><p><strong><code>%in%</code></strong>：判断元素是否存在于向量中。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb175"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb175-1"><a href="#cb175-1" aria-hidden="true" tabindex="-1"></a><span class="dv">1</span> <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>)  <span class="co"># TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div></li>
<li><p><strong><code>unique</code></strong>：找出互不相同的元素。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb177"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb177-1"><a href="#cb177-1" aria-hidden="true" tabindex="-1"></a><span class="fu">unique</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">3</span>))  <span class="co"># [1] 1 2 3</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3</code></pre>
</div>
</div></li>
<li><p><strong><code>which</code></strong>：找到真值的下标集合。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb179"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb179-1"><a href="#cb179-1" aria-hidden="true" tabindex="-1"></a><span class="fu">which</span>(<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>, <span class="cn">TRUE</span>))  <span class="co"># [1] 1 3</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 3</code></pre>
</div>
</div></li>
<li><p><strong><code>duplicated</code></strong>：找到重复元素。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb181"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb181-1"><a href="#cb181-1" aria-hidden="true" tabindex="-1"></a><span class="fu">duplicated</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">3</span>))  <span class="co"># [1] FALSE FALSE TRUE FALSE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE FALSE  TRUE FALSE</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
</section>
<section id="程序设计-2" class="level2">
<h2 class="anchored" data-anchor-id="程序设计-2">程序设计</h2>
<section id="一控制结构-1" class="level3">
<h3 class="anchored" data-anchor-id="一控制结构-1">一、控制结构</h3>
<section id="分支" class="level4">
<h4 class="anchored" data-anchor-id="分支">分支</h4>
<ul>
<li><p><strong><code>if</code>、<code>else</code></strong>：条件分支。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb183"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb183-1"><a href="#cb183-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="dv">1</span> <span class="sc">&lt;</span> <span class="dv">2</span>) {</span>
<span id="cb183-2"><a href="#cb183-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(<span class="st">"1 is less than 2"</span>)</span>
<span id="cb183-3"><a href="#cb183-3" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb183-4"><a href="#cb183-4" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(<span class="st">"1 is not less than 2"</span>)</span>
<span id="cb183-5"><a href="#cb183-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 is less than 2"</code></pre>
</div>
</div></li>
<li><p><strong><code>ifelse</code></strong>：向量化条件选择。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb185"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb185-1"><a href="#cb185-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ifelse</span>(<span class="dv">1</span> <span class="sc">&lt;</span> <span class="dv">2</span>, <span class="st">"Yes"</span>, <span class="st">"No"</span>)  <span class="co"># "Yes"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Yes"</code></pre>
</div>
</div></li>
<li><p><strong><code>switch</code></strong>：多分支选择。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb187"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb187-1"><a href="#cb187-1" aria-hidden="true" tabindex="-1"></a><span class="cf">switch</span>(<span class="dv">2</span>, <span class="st">"First"</span>, <span class="st">"Second"</span>, <span class="st">"Third"</span>)  <span class="co"># "Second"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Second"</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="循环" class="level4">
<h4 class="anchored" data-anchor-id="循环">循环</h4>
<ul>
<li><p><strong><code>for</code></strong>：循环。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb189"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb189-1"><a href="#cb189-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>) {</span>
<span id="cb189-2"><a href="#cb189-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(i)</span>
<span id="cb189-3"><a href="#cb189-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3
[1] 4
[1] 5</code></pre>
</div>
</div></li>
<li><p><strong><code>while</code></strong>：条件循环。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb191"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb191-1"><a href="#cb191-1" aria-hidden="true" tabindex="-1"></a>i <span class="ot">&lt;-</span> <span class="dv">1</span></span>
<span id="cb191-2"><a href="#cb191-2" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> (i <span class="sc">&lt;=</span> <span class="dv">5</span>) {</span>
<span id="cb191-3"><a href="#cb191-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(i)</span>
<span id="cb191-4"><a href="#cb191-4" aria-hidden="true" tabindex="-1"></a>    i <span class="ot">&lt;-</span> i <span class="sc">+</span> <span class="dv">1</span></span>
<span id="cb191-5"><a href="#cb191-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3
[1] 4
[1] 5</code></pre>
</div>
</div></li>
<li><p><strong><code>repeat</code></strong>：无限循环。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb193"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb193-1"><a href="#cb193-1" aria-hidden="true" tabindex="-1"></a>i <span class="ot">&lt;-</span> <span class="dv">1</span></span>
<span id="cb193-2"><a href="#cb193-2" aria-hidden="true" tabindex="-1"></a><span class="cf">repeat</span> {</span>
<span id="cb193-3"><a href="#cb193-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(i)</span>
<span id="cb193-4"><a href="#cb193-4" aria-hidden="true" tabindex="-1"></a>    i <span class="ot">&lt;-</span> i <span class="sc">+</span> <span class="dv">1</span></span>
<span id="cb193-5"><a href="#cb193-5" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> (i <span class="sc">&gt;</span> <span class="dv">5</span>) <span class="cf">break</span></span>
<span id="cb193-6"><a href="#cb193-6" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3
[1] 4
[1] 5</code></pre>
</div>
</div></li>
<li><p><strong><code>break</code></strong>：跳出循环。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb195"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb195-1"><a href="#cb195-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>) {</span>
<span id="cb195-2"><a href="#cb195-2" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> (i <span class="sc">&gt;</span> <span class="dv">5</span>) <span class="cf">break</span></span>
<span id="cb195-3"><a href="#cb195-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(i)</span>
<span id="cb195-4"><a href="#cb195-4" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3
[1] 4
[1] 5</code></pre>
</div>
</div></li>
<li><p><strong><code>next</code></strong>：跳过当前循环迭代。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb197"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb197-1"><a href="#cb197-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>) {</span>
<span id="cb197-2"><a href="#cb197-2" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> (i <span class="sc">%%</span> <span class="dv">2</span> <span class="sc">==</span> <span class="dv">0</span>) <span class="cf">next</span></span>
<span id="cb197-3"><a href="#cb197-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(i)</span>
<span id="cb197-4"><a href="#cb197-4" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 3
[1] 5
[1] 7
[1] 9</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
</section>
<section id="程序设计-3" class="level2">
<h2 class="anchored" data-anchor-id="程序设计-3">程序设计</h2>
<section id="二函数方面" class="level3">
<h3 class="anchored" data-anchor-id="二函数方面">二、函数方面</h3>
<section id="函数定义" class="level4">
<h4 class="anchored" data-anchor-id="函数定义">函数定义</h4>
<ul>
<li><p><strong><code>function</code></strong>：定义函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb199"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb199-1"><a href="#cb199-1" aria-hidden="true" tabindex="-1"></a>my_function <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
<span id="cb199-2"><a href="#cb199-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">return</span>(x<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb199-3"><a href="#cb199-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb199-4"><a href="#cb199-4" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>(<span class="dv">3</span>)  <span class="co"># 输出 9</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 9</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="函数调用" class="level4">
<h4 class="anchored" data-anchor-id="函数调用">函数调用</h4>
<ul>
<li><p><strong><code>call</code></strong>：函数调用。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb201"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb201-1"><a href="#cb201-1" aria-hidden="true" tabindex="-1"></a><span class="fu">call</span>(<span class="st">"+"</span>, <span class="dv">1</span>, <span class="dv">2</span>)  <span class="co"># 等价于 1 + 2</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>1 + 2</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="调试" class="level4">
<h4 class="anchored" data-anchor-id="调试">调试</h4>
<ul>
<li><p><strong><code>browser</code></strong>：进入调试模式。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb203"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb203-1"><a href="#cb203-1" aria-hidden="true" tabindex="-1"></a>my_function <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
<span id="cb203-2"><a href="#cb203-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">browser</span>()</span>
<span id="cb203-3"><a href="#cb203-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">return</span>(x<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb203-4"><a href="#cb203-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb203-5"><a href="#cb203-5" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>(<span class="dv">3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Called from: my_function(3)
debug: return(x^2)</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 9</code></pre>
</div>
</div></li>
<li><p><strong><code>debug</code></strong>：调试函数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb206"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb206-1"><a href="#cb206-1" aria-hidden="true" tabindex="-1"></a><span class="fu">debug</span>(my_function)</span>
<span id="cb206-2"><a href="#cb206-2" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>(<span class="dv">3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>debugging in: my_function(3)
debug: {
    browser()
    return(x^2)
}
debug: browser()
debug: return(x^2)
exiting from: my_function(3)</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 9</code></pre>
</div>
</div></li>
<li><p><strong><code>traceback</code></strong>：打印错误的调用栈。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb209"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb209-1"><a href="#cb209-1" aria-hidden="true" tabindex="-1"></a><span class="fu">traceback</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>无traceback </code></pre>
</div>
</div></li>
</ul>
</section>
<section id="其他" class="level4">
<h4 class="anchored" data-anchor-id="其他">其他</h4>
<ul>
<li><strong><code>source</code></strong>：调用文件。</li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb211"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb211-1"><a href="#cb211-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">"cat('这是script.R')"</span> <span class="op">&gt;</span> script.R</span>
<span id="cb211-2"><a href="#cb211-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> script.R</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>cat('这是script.R')</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb213"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb213-1"><a href="#cb213-1" aria-hidden="true" tabindex="-1"></a><span class="fu">source</span>(<span class="st">"script.R"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>这是script.R</code></pre>
</div>
</div>
<ul>
<li><p><strong><code>missing</code></strong>：判断参数是否缺失。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb215"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb215-1"><a href="#cb215-1" aria-hidden="true" tabindex="-1"></a>my_function <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
<span id="cb215-2"><a href="#cb215-2" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> (<span class="fu">missing</span>(x)) {</span>
<span id="cb215-3"><a href="#cb215-3" aria-hidden="true" tabindex="-1"></a>        <span class="fu">print</span>(<span class="st">"x is missing"</span>)</span>
<span id="cb215-4"><a href="#cb215-4" aria-hidden="true" tabindex="-1"></a>    } <span class="cf">else</span> {</span>
<span id="cb215-5"><a href="#cb215-5" aria-hidden="true" tabindex="-1"></a>        <span class="fu">print</span>(x)</span>
<span id="cb215-6"><a href="#cb215-6" aria-hidden="true" tabindex="-1"></a>    }</span>
<span id="cb215-7"><a href="#cb215-7" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb215-8"><a href="#cb215-8" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "x is missing"</code></pre>
</div>
</div></li>
<li><p><strong><code>nargs</code></strong>：参数个数。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb217"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb217-1"><a href="#cb217-1" aria-hidden="true" tabindex="-1"></a>my_function <span class="ot">&lt;-</span> <span class="cf">function</span>(...) {</span>
<span id="cb217-2"><a href="#cb217-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(<span class="fu">nargs</span>())</span>
<span id="cb217-3"><a href="#cb217-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb217-4"><a href="#cb217-4" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>)  <span class="co"># 输出 3</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div></li>
<li><p><strong><code>stop</code></strong>：终止函数执行。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb219"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb219-1"><a href="#cb219-1" aria-hidden="true" tabindex="-1"></a>my_function <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
<span id="cb219-2"><a href="#cb219-2" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> (x <span class="sc">&lt;</span> <span class="dv">0</span>) <span class="fu">stop</span>(<span class="st">"x must be positive"</span>)</span>
<span id="cb219-3"><a href="#cb219-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">return</span>(x<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb219-4"><a href="#cb219-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb219-5"><a href="#cb219-5" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>(<span class="dv">1</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb221"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb221-1"><a href="#cb221-1" aria-hidden="true" tabindex="-1"></a><span class="co"># my_function(-1)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>on.exit</code></strong>：指定退出时执行。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb222"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb222-1"><a href="#cb222-1" aria-hidden="true" tabindex="-1"></a>my_function <span class="ot">&lt;-</span> <span class="cf">function</span>() {</span>
<span id="cb222-2"><a href="#cb222-2" aria-hidden="true" tabindex="-1"></a>    <span class="fu">on.exit</span>(<span class="fu">print</span>(<span class="st">"Exiting"</span>))</span>
<span id="cb222-3"><a href="#cb222-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">print</span>(<span class="st">"Inside function"</span>)</span>
<span id="cb222-4"><a href="#cb222-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb222-5"><a href="#cb222-5" aria-hidden="true" tabindex="-1"></a><span class="fu">my_function</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Inside function"
[1] "Exiting"</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
</section>
<section id="输入输出-2" class="level2">
<h2 class="anchored" data-anchor-id="输入输出-2">输入输出</h2>
<section id="三显示对象" class="level3">
<h3 class="anchored" data-anchor-id="三显示对象">三、显示对象</h3>
<ul>
<li><p><strong><code>cat</code></strong>：连接并打印对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb224"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb224-1"><a href="#cb224-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Hello"</span>, <span class="st">"World"</span>, <span class="at">sep =</span> <span class="st">" "</span>)  <span class="co"># 输出 "Hello World"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Hello World</code></pre>
</div>
</div></li>
</ul>
</section>
<section id="四输出到文件" class="level3">
<h3 class="anchored" data-anchor-id="四输出到文件">四、输出到文件</h3>
<ul>
<li><p><strong><code>sink</code></strong>：将输出重定向到文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb226"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb226-1"><a href="#cb226-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sink</span>(<span class="st">"output.txt"</span>)</span>
<span id="cb226-2"><a href="#cb226-2" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(<span class="st">"Hello, World!"</span>)</span>
<span id="cb226-3"><a href="#cb226-3" aria-hidden="true" tabindex="-1"></a><span class="fu">sink</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
</section>
<section id="五保存和读取对象" class="level3">
<h3 class="anchored" data-anchor-id="五保存和读取对象">五、保存和读取对象</h3>
<ul>
<li><p><strong><code>save</code></strong>：保存对象到文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb227"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb227-1"><a href="#cb227-1" aria-hidden="true" tabindex="-1"></a>num_vec<span class="ot">=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">9</span></span>
<span id="cb227-2"><a href="#cb227-2" aria-hidden="true" tabindex="-1"></a><span class="fu">save</span>(num_vec, <span class="at">file =</span> <span class="st">"data.RData"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>load</code></strong>：加载对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb228"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb228-1"><a href="#cb228-1" aria-hidden="true" tabindex="-1"></a><span class="fu">load</span>(<span class="st">"data.RData"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
</section>
<section id="六读取数据" class="level3">
<h3 class="anchored" data-anchor-id="六读取数据">六、读取数据</h3>
<ul>
<li><p><strong><code>read.table</code></strong>：从文本文件读取数据。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb229"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb229-1"><a href="#cb229-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.table</span>(<span class="st">"data.txt"</span>, <span class="at">header =</span> <span class="cn">TRUE</span>, <span class="at">sep =</span> <span class="st">","</span>)</span>
<span id="cb229-2"><a href="#cb229-2" aria-hidden="true" tabindex="-1"></a>data</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>      X Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1     1          5.1         3.5          1.4         0.2     setosa
2     2          4.9         3.0          1.4         0.2     setosa
3     3          4.7         3.2          1.3         0.2     setosa
4     4          4.6         3.1          1.5         0.2     setosa
5     5          5.0         3.6          1.4         0.2     setosa
6     6          5.4         3.9          1.7         0.4     setosa
7     7          4.6         3.4          1.4         0.3     setosa
8     8          5.0         3.4          1.5         0.2     setosa
9     9          4.4         2.9          1.4         0.2     setosa
10   10          4.9         3.1          1.5         0.1     setosa
11   11          5.4         3.7          1.5         0.2     setosa
12   12          4.8         3.4          1.6         0.2     setosa
13   13          4.8         3.0          1.4         0.1     setosa
14   14          4.3         3.0          1.1         0.1     setosa
15   15          5.8         4.0          1.2         0.2     setosa
16   16          5.7         4.4          1.5         0.4     setosa
17   17          5.4         3.9          1.3         0.4     setosa
18   18          5.1         3.5          1.4         0.3     setosa
19   19          5.7         3.8          1.7         0.3     setosa
20   20          5.1         3.8          1.5         0.3     setosa
21   21          5.4         3.4          1.7         0.2     setosa
22   22          5.1         3.7          1.5         0.4     setosa
23   23          4.6         3.6          1.0         0.2     setosa
24   24          5.1         3.3          1.7         0.5     setosa
25   25          4.8         3.4          1.9         0.2     setosa
26   26          5.0         3.0          1.6         0.2     setosa
27   27          5.0         3.4          1.6         0.4     setosa
28   28          5.2         3.5          1.5         0.2     setosa
29   29          5.2         3.4          1.4         0.2     setosa
30   30          4.7         3.2          1.6         0.2     setosa
31   31          4.8         3.1          1.6         0.2     setosa
32   32          5.4         3.4          1.5         0.4     setosa
33   33          5.2         4.1          1.5         0.1     setosa
34   34          5.5         4.2          1.4         0.2     setosa
35   35          4.9         3.1          1.5         0.2     setosa
36   36          5.0         3.2          1.2         0.2     setosa
37   37          5.5         3.5          1.3         0.2     setosa
38   38          4.9         3.6          1.4         0.1     setosa
39   39          4.4         3.0          1.3         0.2     setosa
40   40          5.1         3.4          1.5         0.2     setosa
41   41          5.0         3.5          1.3         0.3     setosa
42   42          4.5         2.3          1.3         0.3     setosa
43   43          4.4         3.2          1.3         0.2     setosa
44   44          5.0         3.5          1.6         0.6     setosa
45   45          5.1         3.8          1.9         0.4     setosa
46   46          4.8         3.0          1.4         0.3     setosa
47   47          5.1         3.8          1.6         0.2     setosa
48   48          4.6         3.2          1.4         0.2     setosa
49   49          5.3         3.7          1.5         0.2     setosa
50   50          5.0         3.3          1.4         0.2     setosa
51   51          7.0         3.2          4.7         1.4 versicolor
52   52          6.4         3.2          4.5         1.5 versicolor
53   53          6.9         3.1          4.9         1.5 versicolor
54   54          5.5         2.3          4.0         1.3 versicolor
55   55          6.5         2.8          4.6         1.5 versicolor
56   56          5.7         2.8          4.5         1.3 versicolor
57   57          6.3         3.3          4.7         1.6 versicolor
58   58          4.9         2.4          3.3         1.0 versicolor
59   59          6.6         2.9          4.6         1.3 versicolor
60   60          5.2         2.7          3.9         1.4 versicolor
61   61          5.0         2.0          3.5         1.0 versicolor
62   62          5.9         3.0          4.2         1.5 versicolor
63   63          6.0         2.2          4.0         1.0 versicolor
64   64          6.1         2.9          4.7         1.4 versicolor
65   65          5.6         2.9          3.6         1.3 versicolor
66   66          6.7         3.1          4.4         1.4 versicolor
67   67          5.6         3.0          4.5         1.5 versicolor
68   68          5.8         2.7          4.1         1.0 versicolor
69   69          6.2         2.2          4.5         1.5 versicolor
70   70          5.6         2.5          3.9         1.1 versicolor
71   71          5.9         3.2          4.8         1.8 versicolor
72   72          6.1         2.8          4.0         1.3 versicolor
73   73          6.3         2.5          4.9         1.5 versicolor
74   74          6.1         2.8          4.7         1.2 versicolor
75   75          6.4         2.9          4.3         1.3 versicolor
76   76          6.6         3.0          4.4         1.4 versicolor
77   77          6.8         2.8          4.8         1.4 versicolor
78   78          6.7         3.0          5.0         1.7 versicolor
79   79          6.0         2.9          4.5         1.5 versicolor
80   80          5.7         2.6          3.5         1.0 versicolor
81   81          5.5         2.4          3.8         1.1 versicolor
82   82          5.5         2.4          3.7         1.0 versicolor
83   83          5.8         2.7          3.9         1.2 versicolor
84   84          6.0         2.7          5.1         1.6 versicolor
85   85          5.4         3.0          4.5         1.5 versicolor
86   86          6.0         3.4          4.5         1.6 versicolor
87   87          6.7         3.1          4.7         1.5 versicolor
88   88          6.3         2.3          4.4         1.3 versicolor
89   89          5.6         3.0          4.1         1.3 versicolor
90   90          5.5         2.5          4.0         1.3 versicolor
91   91          5.5         2.6          4.4         1.2 versicolor
92   92          6.1         3.0          4.6         1.4 versicolor
93   93          5.8         2.6          4.0         1.2 versicolor
94   94          5.0         2.3          3.3         1.0 versicolor
95   95          5.6         2.7          4.2         1.3 versicolor
96   96          5.7         3.0          4.2         1.2 versicolor
97   97          5.7         2.9          4.2         1.3 versicolor
98   98          6.2         2.9          4.3         1.3 versicolor
99   99          5.1         2.5          3.0         1.1 versicolor
100 100          5.7         2.8          4.1         1.3 versicolor
101 101          6.3         3.3          6.0         2.5  virginica
102 102          5.8         2.7          5.1         1.9  virginica
103 103          7.1         3.0          5.9         2.1  virginica
104 104          6.3         2.9          5.6         1.8  virginica
105 105          6.5         3.0          5.8         2.2  virginica
106 106          7.6         3.0          6.6         2.1  virginica
107 107          4.9         2.5          4.5         1.7  virginica
108 108          7.3         2.9          6.3         1.8  virginica
109 109          6.7         2.5          5.8         1.8  virginica
110 110          7.2         3.6          6.1         2.5  virginica
111 111          6.5         3.2          5.1         2.0  virginica
112 112          6.4         2.7          5.3         1.9  virginica
113 113          6.8         3.0          5.5         2.1  virginica
114 114          5.7         2.5          5.0         2.0  virginica
115 115          5.8         2.8          5.1         2.4  virginica
116 116          6.4         3.2          5.3         2.3  virginica
117 117          6.5         3.0          5.5         1.8  virginica
118 118          7.7         3.8          6.7         2.2  virginica
119 119          7.7         2.6          6.9         2.3  virginica
120 120          6.0         2.2          5.0         1.5  virginica
121 121          6.9         3.2          5.7         2.3  virginica
122 122          5.6         2.8          4.9         2.0  virginica
123 123          7.7         2.8          6.7         2.0  virginica
124 124          6.3         2.7          4.9         1.8  virginica
125 125          6.7         3.3          5.7         2.1  virginica
126 126          7.2         3.2          6.0         1.8  virginica
127 127          6.2         2.8          4.8         1.8  virginica
128 128          6.1         3.0          4.9         1.8  virginica
129 129          6.4         2.8          5.6         2.1  virginica
130 130          7.2         3.0          5.8         1.6  virginica
131 131          7.4         2.8          6.1         1.9  virginica
132 132          7.9         3.8          6.4         2.0  virginica
133 133          6.4         2.8          5.6         2.2  virginica
134 134          6.3         2.8          5.1         1.5  virginica
135 135          6.1         2.6          5.6         1.4  virginica
136 136          7.7         3.0          6.1         2.3  virginica
137 137          6.3         3.4          5.6         2.4  virginica
138 138          6.4         3.1          5.5         1.8  virginica
139 139          6.0         3.0          4.8         1.8  virginica
140 140          6.9         3.1          5.4         2.1  virginica
141 141          6.7         3.1          5.6         2.4  virginica
142 142          6.9         3.1          5.1         2.3  virginica
143 143          5.8         2.7          5.1         1.9  virginica
144 144          6.8         3.2          5.9         2.3  virginica
145 145          6.7         3.3          5.7         2.5  virginica
146 146          6.7         3.0          5.2         2.3  virginica
147 147          6.3         2.5          5.0         1.9  virginica
148 148          6.5         3.0          5.2         2.0  virginica
149 149          6.2         3.4          5.4         2.3  virginica
150 150          5.9         3.0          5.1         1.8  virginica</code></pre>
</div>
</div></li>
<li><p><strong><code>scan</code></strong>：从文件或键盘读取数据。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb231"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb231-1"><a href="#cb231-1" aria-hidden="true" tabindex="-1"></a><span class="co"># scan("data.txt", what = integer())</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="文件操作-3" class="level2">
<h2 class="anchored" data-anchor-id="文件操作-3">文件操作</h2>
<section id="三文件操作-1" class="level3">
<h3 class="anchored" data-anchor-id="三文件操作-1">三、文件操作</h3>
<ul>
<li><p><strong><code>file.rename</code></strong>：重命名文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb232"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb232-1"><a href="#cb232-1" aria-hidden="true" tabindex="-1"></a><span class="co"># file.rename("old_name.txt", "new_name.txt")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>file.append</code></strong>：向文件追加内容。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb233"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb233-1"><a href="#cb233-1" aria-hidden="true" tabindex="-1"></a><span class="co"># file.append("file1.txt", "file2.txt")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>file.show</code></strong>：显示文件内容。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb234"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb234-1"><a href="#cb234-1" aria-hidden="true" tabindex="-1"></a><span class="co"># file.show("script.R")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>file.info</code></strong>：获取文件信息。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb235"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb235-1"><a href="#cb235-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.info</span>(<span class="st">"script.R"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>         size isdir mode               mtime               ctime
script.R   22 FALSE  644 2025-02-22 20:58:27 2025-02-22 20:58:27
                       atime  uid  gid   uname  grname
script.R 2025-02-22 20:58:27 1000 1000 aiyanga aiyanga</code></pre>
</div>
</div></li>
<li><p><strong><code>file.edit</code></strong>：编辑文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb237"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb237-1"><a href="#cb237-1" aria-hidden="true" tabindex="-1"></a><span class="co"># file.edit("script.R")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>unzip</code></strong>：解压缩文件。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb238"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb238-1"><a href="#cb238-1" aria-hidden="true" tabindex="-1"></a><span class="co"># unzip("archive.zip")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="统计分析-1" class="level2">
<h2 class="anchored" data-anchor-id="统计分析-1">统计分析</h2>
<section id="四多元分析" class="level3">
<h3 class="anchored" data-anchor-id="四多元分析">四、多元分析</h3>
<ul>
<li><p><strong><code>cor</code></strong>：计算相关系数矩阵。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb239"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb239-1"><a href="#cb239-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(mtcars)  <span class="co"># 计算mtcars数据集的相关系数矩阵</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>            mpg        cyl       disp         hp        drat         wt
mpg   1.0000000 -0.8521620 -0.8475514 -0.7761684  0.68117191 -0.8676594
cyl  -0.8521620  1.0000000  0.9020329  0.8324475 -0.69993811  0.7824958
disp -0.8475514  0.9020329  1.0000000  0.7909486 -0.71021393  0.8879799
hp   -0.7761684  0.8324475  0.7909486  1.0000000 -0.44875912  0.6587479
drat  0.6811719 -0.6999381 -0.7102139 -0.4487591  1.00000000 -0.7124406
wt   -0.8676594  0.7824958  0.8879799  0.6587479 -0.71244065  1.0000000
qsec  0.4186840 -0.5912421 -0.4336979 -0.7082234  0.09120476 -0.1747159
vs    0.6640389 -0.8108118 -0.7104159 -0.7230967  0.44027846 -0.5549157
am    0.5998324 -0.5226070 -0.5912270 -0.2432043  0.71271113 -0.6924953
gear  0.4802848 -0.4926866 -0.5555692 -0.1257043  0.69961013 -0.5832870
carb -0.5509251  0.5269883  0.3949769  0.7498125 -0.09078980  0.4276059
            qsec         vs          am       gear        carb
mpg   0.41868403  0.6640389  0.59983243  0.4802848 -0.55092507
cyl  -0.59124207 -0.8108118 -0.52260705 -0.4926866  0.52698829
disp -0.43369788 -0.7104159 -0.59122704 -0.5555692  0.39497686
hp   -0.70822339 -0.7230967 -0.24320426 -0.1257043  0.74981247
drat  0.09120476  0.4402785  0.71271113  0.6996101 -0.09078980
wt   -0.17471588 -0.5549157 -0.69249526 -0.5832870  0.42760594
qsec  1.00000000  0.7445354 -0.22986086 -0.2126822 -0.65624923
vs    0.74453544  1.0000000  0.16834512  0.2060233 -0.56960714
am   -0.22986086  0.1683451  1.00000000  0.7940588  0.05753435
gear -0.21268223  0.2060233  0.79405876  1.0000000  0.27407284
carb -0.65624923 -0.5696071  0.05753435  0.2740728  1.00000000</code></pre>
</div>
</div></li>
<li><p><strong><code>princomp</code></strong>：主成分分析。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb241"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb241-1"><a href="#cb241-1" aria-hidden="true" tabindex="-1"></a>pca <span class="ot">&lt;-</span> <span class="fu">princomp</span>(mtcars, <span class="at">cor =</span> <span class="cn">TRUE</span>)</span>
<span id="cb241-2"><a href="#cb241-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(pca)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Importance of components:
                          Comp.1    Comp.2     Comp.3     Comp.4     Comp.5
Standard deviation     2.5706809 1.6280258 0.79195787 0.51922773 0.47270615
Proportion of Variance 0.6007637 0.2409516 0.05701793 0.02450886 0.02031374
Cumulative Proportion  0.6007637 0.8417153 0.89873322 0.92324208 0.94355581
                           Comp.6     Comp.7     Comp.8      Comp.9     Comp.10
Standard deviation     0.45999578 0.36777981 0.35057301 0.277572792 0.228112781
Proportion of Variance 0.01923601 0.01229654 0.01117286 0.007004241 0.004730495
Cumulative Proportion  0.96279183 0.97508837 0.98626123 0.993265468 0.997995963
                           Comp.11
Standard deviation     0.148473587
Proportion of Variance 0.002004037
Cumulative Proportion  1.000000000</code></pre>
</div>
</div></li>
<li><p><strong><code>hclust</code></strong>：层次聚类。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb243"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb243-1"><a href="#cb243-1" aria-hidden="true" tabindex="-1"></a>dist_matrix <span class="ot">&lt;-</span> <span class="fu">dist</span>(mtcars)</span>
<span id="cb243-2"><a href="#cb243-2" aria-hidden="true" tabindex="-1"></a>hc <span class="ot">&lt;-</span> <span class="fu">hclust</span>(dist_matrix)</span>
<span id="cb243-3"><a href="#cb243-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(hc)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="R语言常用函数_files/figure-html/unnamed-chunk-123-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div></li>
</ul>
</section>
<section id="五时间序列" class="level3">
<h3 class="anchored" data-anchor-id="五时间序列">五、时间序列</h3>
<ul>
<li><p><strong><code>ts</code></strong>：创建时间序列对象。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb244"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb244-1"><a href="#cb244-1" aria-hidden="true" tabindex="-1"></a>ts_data <span class="ot">&lt;-</span> <span class="fu">ts</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,</span>
<span id="cb244-2"><a href="#cb244-2" aria-hidden="true" tabindex="-1"></a> <span class="at">frequency =</span> <span class="dv">4</span>)</span>
<span id="cb244-3"><a href="#cb244-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(ts_data)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="R语言常用函数_files/figure-html/unnamed-chunk-124-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div></li>
</ul>
</section>
<section id="六统计模型" class="level3">
<h3 class="anchored" data-anchor-id="六统计模型">六、统计模型</h3>
<ul>
<li><p><strong><code>lm</code></strong>：线性模型。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb245"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb245-1"><a href="#cb245-1" aria-hidden="true" tabindex="-1"></a>model <span class="ot">&lt;-</span> <span class="fu">lm</span>(mpg <span class="sc">~</span> wt <span class="sc">+</span> hp, <span class="at">data =</span> mtcars)</span>
<span id="cb245-2"><a href="#cb245-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(model)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = mpg ~ wt + hp, data = mtcars)

Residuals:
   Min     1Q Median     3Q    Max 
-3.941 -1.600 -0.182  1.050  5.854 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) 37.22727    1.59879  23.285  &lt; 2e-16 ***
wt          -3.87783    0.63273  -6.129 1.12e-06 ***
hp          -0.03177    0.00903  -3.519  0.00145 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.593 on 29 degrees of freedom
Multiple R-squared:  0.8268,    Adjusted R-squared:  0.8148 
F-statistic: 69.21 on 2 and 29 DF,  p-value: 9.109e-12</code></pre>
</div>
</div></li>
<li><p><strong><code>glm</code></strong>：广义线性模型。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb247"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb247-1"><a href="#cb247-1" aria-hidden="true" tabindex="-1"></a>glm_model <span class="ot">&lt;-</span> <span class="fu">glm</span>(mpg <span class="sc">~</span> wt <span class="sc">+</span> hp, <span class="at">data =</span> mtcars, <span class="at">family =</span> gaussian)</span>
<span id="cb247-2"><a href="#cb247-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(glm_model)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = mpg ~ wt + hp, family = gaussian, data = mtcars)

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) 37.22727    1.59879  23.285  &lt; 2e-16 ***
wt          -3.87783    0.63273  -6.129 1.12e-06 ***
hp          -0.03177    0.00903  -3.519  0.00145 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 6.725785)

    Null deviance: 1126.05  on 31  degrees of freedom
Residual deviance:  195.05  on 29  degrees of freedom
AIC: 156.65

Number of Fisher Scoring iterations: 2</code></pre>
</div>
</div></li>
<li><p><strong><code>aov</code></strong>：方差分析。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb249"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb249-1"><a href="#cb249-1" aria-hidden="true" tabindex="-1"></a>aov_model <span class="ot">&lt;-</span> <span class="fu">aov</span>(mpg <span class="sc">~</span> <span class="fu">factor</span>(cyl), <span class="at">data =</span> mtcars)</span>
<span id="cb249-2"><a href="#cb249-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(aov_model)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>            Df Sum Sq Mean Sq F value   Pr(&gt;F)    
factor(cyl)  2  824.8   412.4    39.7 4.98e-09 ***
Residuals   29  301.3    10.4                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="工作环境-1" class="level2">
<h2 class="anchored" data-anchor-id="工作环境-1">工作环境</h2>
<section id="三帮助功能" class="level3">
<h3 class="anchored" data-anchor-id="三帮助功能">三、帮助功能</h3>
<ul>
<li><p><strong><code>?</code></strong>：获取帮助。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb251"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb251-1"><a href="#cb251-1" aria-hidden="true" tabindex="-1"></a>?lm</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>help</code></strong>：获取帮助。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb252"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb252-1"><a href="#cb252-1" aria-hidden="true" tabindex="-1"></a><span class="fu">help</span>(<span class="st">"lm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>help.start</code></strong>：启动R的帮助系统。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb253"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb253-1"><a href="#cb253-1" aria-hidden="true" tabindex="-1"></a><span class="co"># help.start()</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
<li><p><strong><code>apropos</code></strong>：搜索函数名。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb254"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb254-1"><a href="#cb254-1" aria-hidden="true" tabindex="-1"></a><span class="fu">apropos</span>(<span class="st">"lm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] ".colMeans"       ".lm.fit"         "colMeans"        "confint.lm"     
 [5] "contr.helmert"   "dummy.coef.lm"   "glm"             "glm_model"      
 [9] "glm.control"     "glm.fit"         "KalmanForecast"  "KalmanLike"     
[13] "KalmanRun"       "KalmanSmooth"    "kappa.lm"        "lm"             
[17] "lm.fit"          "lm.influence"    "lm.wfit"         "model.matrix.lm"
[21] "nlm"             "nlminb"          "predict.glm"     "predict.lm"     
[25] "residuals.glm"   "residuals.lm"    "summary.glm"     "summary.lm"     </code></pre>
</div>
</div></li>
</ul>
</section>
<section id="四数据集" class="level3">
<h3 class="anchored" data-anchor-id="四数据集">四、数据集</h3>
<ul>
<li><p><strong><code>data</code></strong>：列出或加载数据集。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb256"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb256-1"><a href="#cb256-1" aria-hidden="true" tabindex="-1"></a><span class="co"># data()  # 列出所有可用数据集</span></span>
<span id="cb256-2"><a href="#cb256-2" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(mtcars)  <span class="co"># 加载mtcars数据集</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></li>
</ul>
</section>
</section>
<section id="统计分析-2" class="level2">
<h2 class="anchored" data-anchor-id="统计分析-2">统计分析</h2>
<section id="四多元分析续" class="level3">
<h3 class="anchored" data-anchor-id="四多元分析续">四、多元分析（续）</h3>
<section id="典则相关分析" class="level4">
<h4 class="anchored" data-anchor-id="典则相关分析">典则相关分析</h4>
<ul>
<li><p><strong><code>cancor</code></strong>：典则相关分析。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb257"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb257-1"><a href="#cb257-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 假设有两个数据集 X 和 Y</span></span>
<span id="cb257-2"><a href="#cb257-2" aria-hidden="true" tabindex="-1"></a>X <span class="ot">&lt;-</span> mtcars[, <span class="fu">c</span>(<span class="st">"mpg"</span>, <span class="st">"wt"</span>)]</span>
<span id="cb257-3"><a href="#cb257-3" aria-hidden="true" tabindex="-1"></a>Y <span class="ot">&lt;-</span> mtcars[, <span class="fu">c</span>(<span class="st">"hp"</span>, <span class="st">"disp"</span>)]</span>
<span id="cb257-4"><a href="#cb257-4" aria-hidden="true" tabindex="-1"></a>cancor_result <span class="ot">&lt;-</span> <span class="fu">cancor</span>(X, Y)</span>
<span id="cb257-5"><a href="#cb257-5" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(cancor_result)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$cor
[1] 0.9015095 0.4773130

$xcoef
           [,1]       [,2]
mpg  0.01084157 0.05895271
wt  -0.12258972 0.34827147

$ycoef
              [,1]         [,2]
hp   -7.274593e-05 -0.004280548
disp -1.417107e-03  0.001897589

$xcenter
     mpg       wt 
20.09062  3.21725 

$ycenter
      hp     disp 
146.6875 230.7219 </code></pre>
</div>
</div></li>
</ul>
</section>
<section id="主成分分析续" class="level4">
<h4 class="anchored" data-anchor-id="主成分分析续">主成分分析（续）</h4>
<ul>
<li><p><strong><code>biplot</code></strong>：绘制主成分分析的双标图。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb259"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb259-1"><a href="#cb259-1" aria-hidden="true" tabindex="-1"></a><span class="fu">biplot</span>(pca)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="R语言常用函数_files/figure-html/unnamed-chunk-134-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div></li>
</ul>
</section>
<section id="聚类分析" class="level4">
<h4 class="anchored" data-anchor-id="聚类分析">聚类分析</h4>
<ul>
<li><p><strong><code>kmeans</code></strong>：K-均值聚类。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb260"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb260-1"><a href="#cb260-1" aria-hidden="true" tabindex="-1"></a>kmeans_result <span class="ot">&lt;-</span> <span class="fu">kmeans</span>(mtcars, <span class="at">centers =</span> <span class="dv">3</span>)</span>
<span id="cb260-2"><a href="#cb260-2" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(kmeans_result<span class="sc">$</span>cluster)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>          Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
                  1                   1                   3                   1 
  Hornet Sportabout             Valiant          Duster 360           Merc 240D 
                  2                   1                   2                   3 
           Merc 230            Merc 280           Merc 280C          Merc 450SE 
                  3                   1                   1                   2 
         Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
                  2                   2                   2                   2 
  Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
                  2                   3                   3                   3 
      Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
                  3                   2                   2                   2 
   Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
                  2                   3                   3                   3 
     Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
                  2                   1                   2                   3 </code></pre>
</div>
</div></li>
</ul>
</section>
<section id="多维标度分析" class="level4">
<h4 class="anchored" data-anchor-id="多维标度分析">多维标度分析</h4>
<ul>
<li><p><strong><code>cmdscale</code></strong>：经典多维标度分析。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb262"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb262-1"><a href="#cb262-1" aria-hidden="true" tabindex="-1"></a>dist_matrix <span class="ot">&lt;-</span> <span class="fu">dist</span>(mtcars)</span>
<span id="cb262-2"><a href="#cb262-2" aria-hidden="true" tabindex="-1"></a>mds_result <span class="ot">&lt;-</span> <span class="fu">cmdscale</span>(dist_matrix)</span>
<span id="cb262-3"><a href="#cb262-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(mds_result)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="R语言常用函数_files/figure-html/unnamed-chunk-136-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
</section>
<section id="时间序列分析" class="level2">
<h2 class="anchored" data-anchor-id="时间序列分析">时间序列分析</h2>
<section id="五时间序列续" class="level3">
<h3 class="anchored" data-anchor-id="五时间序列续">五、时间序列（续）</h3>
<ul>
<li><p><strong><code>diff</code></strong>：计算差分。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb263"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb263-1"><a href="#cb263-1" aria-hidden="true" tabindex="-1"></a>diff_ts <span class="ot">&lt;-</span> <span class="fu">diff</span>(ts_data, <span class="at">differences =</span> <span class="dv">1</span>)</span>
<span id="cb263-2"><a href="#cb263-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(diff_ts)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="R语言常用函数_files/figure-html/unnamed-chunk-137-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div></li>
<li><p><strong><code>window</code></strong>：提取时间序列的子集。</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb264"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb264-1"><a href="#cb264-1" aria-hidden="true" tabindex="-1"></a><span class="fu">window</span>(ts_data, <span class="at">start =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>), <span class="at">end =</span> <span class="fu">c</span>(<span class="dv">5</span>, <span class="dv">4</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in window.default(x, ...): 'end'值没有变化</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  Qtr1 Qtr2 Qtr3 Qtr4
1    1    2    3    4
2    5    6    7    8
3    9   10          </code></pre>
</div>
</div></li>
</ul>
<hr>
</section>
</section>
<section id="文件操作-4" class="level2">
<h2 class="anchored" data-anchor-id="文件操作-4">文件操作</h2>
<section id="四清理创建的演示文件" class="level3">
<h3 class="anchored" data-anchor-id="四清理创建的演示文件">四、清理创建的演示文件</h3>
<p>在完成所有演示后，建议清理创建的文件和对象，以保持工作环境的整洁。以下是一些清理代码：</p>
<section id="删除创建的文件" class="level4">
<h4 class="anchored" data-anchor-id="删除创建的文件">删除创建的文件</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb267"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb267-1"><a href="#cb267-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 删除文件</span></span>
<span id="cb267-2"><a href="#cb267-2" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"data.RData"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb269"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb269-1"><a href="#cb269-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"archive.zip"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb271"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb271-1"><a href="#cb271-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"output.txt"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb273"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb273-1"><a href="#cb273-1" aria-hidden="true" tabindex="-1"></a><span class="co"># file.remove("file.jpg")</span></span>
<span id="cb273-2"><a href="#cb273-2" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"data.txt"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb275"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb275-1"><a href="#cb275-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"file1.txt"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb277"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb277-1"><a href="#cb277-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"file2.txt"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb279"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb279-1"><a href="#cb279-1" aria-hidden="true" tabindex="-1"></a><span class="fu">file.remove</span>(<span class="st">"script.R"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
</section>
<section id="删除创建的文件夹" class="level4">
<h4 class="anchored" data-anchor-id="删除创建的文件夹">删除创建的文件夹</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb281"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb281-1"><a href="#cb281-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 删除文件夹</span></span>
<span id="cb281-2"><a href="#cb281-2" aria-hidden="true" tabindex="-1"></a><span class="fu">unlink</span>(<span class="st">"new_folder"</span>, <span class="at">recursive =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="清理r环境中的对象" class="level4">
<h4 class="anchored" data-anchor-id="清理r环境中的对象">清理R环境中的对象</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb282"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb282-1"><a href="#cb282-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 删除所有对象</span></span>
<span id="cb282-2"><a href="#cb282-2" aria-hidden="true" tabindex="-1"></a><span class="fu">rm</span>(<span class="at">list =</span> <span class="fu">ls</span>())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<hr>
</section>
</section>
</section>

</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
  const toggleBodyColorMode = (bsSheetEl) => {
    const mode = bsSheetEl.getAttribute("data-mode");
    const bodyEl = window.document.querySelector("body");
    if (mode === "dark") {
      bodyEl.classList.add("quarto-dark");
      bodyEl.classList.remove("quarto-light");
    } else {
      bodyEl.classList.add("quarto-light");
      bodyEl.classList.remove("quarto-dark");
    }
  }
  const toggleBodyColorPrimary = () => {
    const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
    if (bsSheetEl) {
      toggleBodyColorMode(bsSheetEl);
    }
  }
  toggleBodyColorPrimary();  
  const icon = "";
  const anchorJS = new window.AnchorJS();
  anchorJS.options = {
    placement: 'right',
    icon: icon
  };
  anchorJS.add('.anchored');
  const isCodeAnnotation = (el) => {
    for (const clz of el.classList) {
      if (clz.startsWith('code-annotation-')) {                     
        return true;
      }
    }
    return false;
  }
  const onCopySuccess = function(e) {
    // button target
    const button = e.trigger;
    // don't keep focus
    button.blur();
    // flash "checked"
    button.classList.add('code-copy-button-checked');
    var currentTitle = button.getAttribute("title");
    button.setAttribute("title", "Copied!");
    let tooltip;
    if (window.bootstrap) {
      button.setAttribute("data-bs-toggle", "tooltip");
      button.setAttribute("data-bs-placement", "left");
      button.setAttribute("data-bs-title", "Copied!");
      tooltip = new bootstrap.Tooltip(button, 
        { trigger: "manual", 
          customClass: "code-copy-button-tooltip",
          offset: [0, -8]});
      tooltip.show();    
    }
    setTimeout(function() {
      if (tooltip) {
        tooltip.hide();
        button.removeAttribute("data-bs-title");
        button.removeAttribute("data-bs-toggle");
        button.removeAttribute("data-bs-placement");
      }
      button.setAttribute("title", currentTitle);
      button.classList.remove('code-copy-button-checked');
    }, 1000);
    // clear code selection
    e.clearSelection();
  }
  const getTextToCopy = function(trigger) {
      const codeEl = trigger.previousElementSibling.cloneNode(true);
      for (const childEl of codeEl.children) {
        if (isCodeAnnotation(childEl)) {
          childEl.remove();
        }
      }
      return codeEl.innerText;
  }
  const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
    text: getTextToCopy
  });
  clipboard.on('success', onCopySuccess);
  if (window.document.getElementById('quarto-embedded-source-code-modal')) {
    const clipboardModal = new window.ClipboardJS('.code-copy-button[data-in-quarto-modal]', {
      text: getTextToCopy,
      container: window.document.getElementById('quarto-embedded-source-code-modal')
    });
    clipboardModal.on('success', onCopySuccess);
  }
    var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
    var mailtoRegex = new RegExp(/^mailto:/);
      var filterRegex = new RegExp('/' + window.location.host + '/');
    var isInternal = (href) => {
        return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
    }
    // Inspect non-navigation links and adorn them if external
 	var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool):not(.about-link)');
    for (var i=0; i<links.length; i++) {
      const link = links[i];
      if (!isInternal(link.href)) {
        // undo the damage that might have been done by quarto-nav.js in the case of
        // links that we want to consider external
        if (link.dataset.originalHref !== undefined) {
          link.href = link.dataset.originalHref;
        }
      }
    }
  function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
    const config = {
      allowHTML: true,
      maxWidth: 500,
      delay: 100,
      arrow: false,
      appendTo: function(el) {
          return el.parentElement;
      },
      interactive: true,
      interactiveBorder: 10,
      theme: 'quarto',
      placement: 'bottom-start',
    };
    if (contentFn) {
      config.content = contentFn;
    }
    if (onTriggerFn) {
      config.onTrigger = onTriggerFn;
    }
    if (onUntriggerFn) {
      config.onUntrigger = onUntriggerFn;
    }
    window.tippy(el, config); 
  }
  const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
  for (var i=0; i<noterefs.length; i++) {
    const ref = noterefs[i];
    tippyHover(ref, function() {
      // use id or data attribute instead here
      let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
      try { href = new URL(href).hash; } catch {}
      const id = href.replace(/^#\/?/, "");
      const note = window.document.getElementById(id);
      if (note) {
        return note.innerHTML;
      } else {
        return "";
      }
    });
  }
  const xrefs = window.document.querySelectorAll('a.quarto-xref');
  const processXRef = (id, note) => {
    // Strip column container classes
    const stripColumnClz = (el) => {
      el.classList.remove("page-full", "page-columns");
      if (el.children) {
        for (const child of el.children) {
          stripColumnClz(child);
        }
      }
    }
    stripColumnClz(note)
    if (id === null || id.startsWith('sec-')) {
      // Special case sections, only their first couple elements
      const container = document.createElement("div");
      if (note.children && note.children.length > 2) {
        container.appendChild(note.children[0].cloneNode(true));
        for (let i = 1; i < note.children.length; i++) {
          const child = note.children[i];
          if (child.tagName === "P" && child.innerText === "") {
            continue;
          } else {
            container.appendChild(child.cloneNode(true));
            break;
          }
        }
        if (window.Quarto?.typesetMath) {
          window.Quarto.typesetMath(container);
        }
        return container.innerHTML
      } else {
        if (window.Quarto?.typesetMath) {
          window.Quarto.typesetMath(note);
        }
        return note.innerHTML;
      }
    } else {
      // Remove any anchor links if they are present
      const anchorLink = note.querySelector('a.anchorjs-link');
      if (anchorLink) {
        anchorLink.remove();
      }
      if (window.Quarto?.typesetMath) {
        window.Quarto.typesetMath(note);
      }
      if (note.classList.contains("callout")) {
        return note.outerHTML;
      } else {
        return note.innerHTML;
      }
    }
  }
  for (var i=0; i<xrefs.length; i++) {
    const xref = xrefs[i];
    tippyHover(xref, undefined, function(instance) {
      instance.disable();
      let url = xref.getAttribute('href');
      let hash = undefined; 
      if (url.startsWith('#')) {
        hash = url;
      } else {
        try { hash = new URL(url).hash; } catch {}
      }
      if (hash) {
        const id = hash.replace(/^#\/?/, "");
        const note = window.document.getElementById(id);
        if (note !== null) {
          try {
            const html = processXRef(id, note.cloneNode(true));
            instance.setContent(html);
          } finally {
            instance.enable();
            instance.show();
          }
        } else {
          // See if we can fetch this
          fetch(url.split('#')[0])
          .then(res => res.text())
          .then(html => {
            const parser = new DOMParser();
            const htmlDoc = parser.parseFromString(html, "text/html");
            const note = htmlDoc.getElementById(id);
            if (note !== null) {
              const html = processXRef(id, note);
              instance.setContent(html);
            } 
          }).finally(() => {
            instance.enable();
            instance.show();
          });
        }
      } else {
        // See if we can fetch a full url (with no hash to target)
        // This is a special case and we should probably do some content thinning / targeting
        fetch(url)
        .then(res => res.text())
        .then(html => {
          const parser = new DOMParser();
          const htmlDoc = parser.parseFromString(html, "text/html");
          const note = htmlDoc.querySelector('main.content');
          if (note !== null) {
            // This should only happen for chapter cross references
            // (since there is no id in the URL)
            // remove the first header
            if (note.children.length > 0 && note.children[0].tagName === "HEADER") {
              note.children[0].remove();
            }
            const html = processXRef(null, note);
            instance.setContent(html);
          } 
        }).finally(() => {
          instance.enable();
          instance.show();
        });
      }
    }, function(instance) {
    });
  }
      let selectedAnnoteEl;
      const selectorForAnnotation = ( cell, annotation) => {
        let cellAttr = 'data-code-cell="' + cell + '"';
        let lineAttr = 'data-code-annotation="' +  annotation + '"';
        const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
        return selector;
      }
      const selectCodeLines = (annoteEl) => {
        const doc = window.document;
        const targetCell = annoteEl.getAttribute("data-target-cell");
        const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
        const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
        const lines = annoteSpan.getAttribute("data-code-lines").split(",");
        const lineIds = lines.map((line) => {
          return targetCell + "-" + line;
        })
        let top = null;
        let height = null;
        let parent = null;
        if (lineIds.length > 0) {
            //compute the position of the single el (top and bottom and make a div)
            const el = window.document.getElementById(lineIds[0]);
            top = el.offsetTop;
            height = el.offsetHeight;
            parent = el.parentElement.parentElement;
          if (lineIds.length > 1) {
            const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
            const bottom = lastEl.offsetTop + lastEl.offsetHeight;
            height = bottom - top;
          }
          if (top !== null && height !== null && parent !== null) {
            // cook up a div (if necessary) and position it 
            let div = window.document.getElementById("code-annotation-line-highlight");
            if (div === null) {
              div = window.document.createElement("div");
              div.setAttribute("id", "code-annotation-line-highlight");
              div.style.position = 'absolute';
              parent.appendChild(div);
            }
            div.style.top = top - 2 + "px";
            div.style.height = height + 4 + "px";
            div.style.left = 0;
            let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
            if (gutterDiv === null) {
              gutterDiv = window.document.createElement("div");
              gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
              gutterDiv.style.position = 'absolute';
              const codeCell = window.document.getElementById(targetCell);
              const gutter = codeCell.querySelector('.code-annotation-gutter');
              gutter.appendChild(gutterDiv);
            }
            gutterDiv.style.top = top - 2 + "px";
            gutterDiv.style.height = height + 4 + "px";
          }
          selectedAnnoteEl = annoteEl;
        }
      };
      const unselectCodeLines = () => {
        const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
        elementsIds.forEach((elId) => {
          const div = window.document.getElementById(elId);
          if (div) {
            div.remove();
          }
        });
        selectedAnnoteEl = undefined;
      };
        // Handle positioning of the toggle
    window.addEventListener(
      "resize",
      throttle(() => {
        elRect = undefined;
        if (selectedAnnoteEl) {
          selectCodeLines(selectedAnnoteEl);
        }
      }, 10)
    );
    function throttle(fn, ms) {
    let throttle = false;
    let timer;
      return (...args) => {
        if(!throttle) { // first call gets through
            fn.apply(this, args);
            throttle = true;
        } else { // all the others get throttled
            if(timer) clearTimeout(timer); // cancel #2
            timer = setTimeout(() => {
              fn.apply(this, args);
              timer = throttle = false;
            }, ms);
        }
      };
    }
      // Attach click handler to the DT
      const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
      for (const annoteDlNode of annoteDls) {
        annoteDlNode.addEventListener('click', (event) => {
          const clickedEl = event.target;
          if (clickedEl !== selectedAnnoteEl) {
            unselectCodeLines();
            const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
            if (activeEl) {
              activeEl.classList.remove('code-annotation-active');
            }
            selectCodeLines(clickedEl);
            clickedEl.classList.add('code-annotation-active');
          } else {
            // Unselect the line
            unselectCodeLines();
            clickedEl.classList.remove('code-annotation-active');
          }
        });
      }
  const findCites = (el) => {
    const parentEl = el.parentElement;
    if (parentEl) {
      const cites = parentEl.dataset.cites;
      if (cites) {
        return {
          el,
          cites: cites.split(' ')
        };
      } else {
        return findCites(el.parentElement)
      }
    } else {
      return undefined;
    }
  };
  var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
  for (var i=0; i<bibliorefs.length; i++) {
    const ref = bibliorefs[i];
    const citeInfo = findCites(ref);
    if (citeInfo) {
      tippyHover(citeInfo.el, function() {
        var popup = window.document.createElement('div');
        citeInfo.cites.forEach(function(cite) {
          var citeDiv = window.document.createElement('div');
          citeDiv.classList.add('hanging-indent');
          citeDiv.classList.add('csl-entry');
          var biblioDiv = window.document.getElementById('ref-' + cite);
          if (biblioDiv) {
            citeDiv.innerHTML = biblioDiv.innerHTML;
          }
          popup.appendChild(citeDiv);
        });
        return popup.innerHTML;
      });
    }
  }
});
</script>
</div> <!-- /content -->




</body></html>